I want to add my own security group to my AWS Elastic Beanstalk environment.
Short description
By default, Elastic Beanstalk creates a security group for your environment. To add your own security group, use one of the following methods:
- Add configuration files to an .ebextensions directory in your application deployment package.
- Use the Elastic Beanstalk console to update the environment configuration.
- Use the AWS CLI to update the environment configuration.
Important: VPC configuration determines the security group value format. For non-VPC-configured environments where aws:ec2:vpc/VPCId is empty, use security group names, such as ebtest. For VPC-configured environments where aws:ec2:vpc/VPCId is set, use security group IDs, such as sg-0123456789abcdef0. If you use security group IDs in a non-VPC-configured environment, you receive the error "The security group 'sg-...' does not exist." You can't migrate an existing environment to a VPC. To use security group IDs, launch a new environment with a VPC configured.
The SecurityGroups option accepts a comma-separated list of values to attach multiple security groups to your Amazon EC2 instances. On Amazon Linux 2 and Amazon Linux 2023 platforms, Elastic Beanstalk retains the default managed security group on your instances even when you specify custom security groups. To remove the default security group, set the DisableDefaultEC2SecurityGroup option to true in the aws:autoscaling:launchconfiguration namespace.
Resolution
To add your own security group to your Elastic Beanstalk environment, choose one of the following methods:
Use .ebextensions configuration files
Complete the following steps:
-
Use an existing security group, or create a new security group.
-
Record the name or ID of your security group based on your environment's VPC configuration.
-
Create an .ebextensions/ directory in your local application code directory.
Note: For more information, see Advanced environment customization with configuration files (.ebextensions).
-
In the .ebextensions/ directory, create a file named securitygroup.config.
-
For a non-VPC-configured environment, enter the following settings:
option_settings:
- namespace: aws:autoscaling:launchconfiguration
option_name: SecurityGroups
value: ebtest
Note: Replace ebtest with the name of your security group.
-
For a VPC-configured environment, enter the following settings:
option_settings:
- namespace: aws:autoscaling:launchconfiguration
option_name: SecurityGroups
value: sg-0123456789abcdef0
Note: Replace sg-0123456789abcdef0 with your security group ID.
-
(Optional) To attach multiple security groups, use a comma-separated list. See the following example:
option_settings:
- namespace: aws:autoscaling:launchconfiguration
option_name: SecurityGroups
value: sg-0123456789abcdef0,sg-0987654321fedcba0
-
Deploy your code and the new .ebextensions/ directory together as a new application version.
The following example shows the structure of the .ebextensions directory and securitygroup.config file in the application zip file:
~/my-app/
|-- .ebextensions/
| |-- securitygroup.config
| `-- other.config
|-- other application files
After deployment, Elastic Beanstalk attaches your security groups to your Amazon EC2 instances. Elastic Beanstalk also retains the default security group unless you set DisableDefaultEC2SecurityGroup to true.
Use the Elastic Beanstalk console
Complete the following steps:
- Open the Elastic Beanstalk console.
- In the navigation pane, choose Environments, and then choose the name of your environment.
- In the navigation pane, choose Configuration.
- In the Instances configuration category, choose Edit.
- Under EC2 security groups, choose the security groups that you want to attach to your instances.
- Choose Apply.
Use the AWS CLI
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.
To add your security group to an existing Elastic Beanstalk environment, run the following update-environment AWS CLI command:
aws elasticbeanstalk update-environment \
--environment-name my-env \
--option-settings '[{"Namespace":"aws:autoscaling:launchconfiguration",
"OptionName":"SecurityGroups",
"Value":"sg-0123456789abcdef0,sg-0987654321fedcba0"}]'
Note: Replace my-env with your environment name. Replace the security group IDs with your own values.
To verify that Elastic Beanstalk applied the security groups, run the following describe-configuration-settings command:
aws elasticbeanstalk describe-configuration-settings \
--environment-name my-env \
--application-name my-app \
--query "ConfigurationSettings[0].OptionSettings[?OptionName=='SecurityGroups']"
Note: Replace my-env with your environment name and my-app with your application name.
Important: Don't reference Elastic Beanstalk-managed security groups from external resources. Elastic Beanstalk-managed security groups belong to the AWS CloudFormation stack that manages your environment. During environment updates, rebuilds, or terminations, AWS CloudFormation might delete and recreate these security groups. If external resources such as Amazon RDS, AWS Lambda, or Amazon ElastiCache reference an Elastic Beanstalk-managed security group, then AWS CloudFormation might fail to delete the security group with a DependencyViolation error. The environment update or rebuild might fail or stop responding. To avoid lifecycle dependencies, create your own security groups independently and attach them to both your Elastic Beanstalk environment and your external resources.
Related information
elastic beanstalk samples on the GitHub website
How do I use my own security group for my load balancer when I deploy an Elastic Beanstalk application?
aws:autoscaling:launchconfiguration
How do I restrict access to SSH port 22 in my Elastic Beanstalk instance security group to a specific CIDR range or security group?
How do I terminate or rebuild my Elastic Beanstalk environment when the AWSEBSecurityGroup fails to delete?