How can I use T2 Unlimited instances with AWS Elastic Beanstalk in my Linux environment? 

AWS doesn't currently support the use of T2 Unlimited instances with Elastic Beanstalk in a Linux environment without the use of surplus credits. To use T2 instances in your Linux environment, you must modify your .ebextensions configuration file in Elastic Beanstalk. 

Update the inline policy for the instance profile of your Elastic Beanstalk environment

If your Elastic Beanstalk environment is configured to use a different instance profile, complete the following steps to add an inline policy to the instance profile associated with your environment. The example below adds an inline policy to aws-elasticbeanstalk-ec2-role, which is the default Elastic Beanstalk instance profile.

1.    Open the AWS Identity and Access Management (IAM) console.

2.    Choose Roles.

3.    In the search box, enter aws-elasticbeanstalk-ec2-role to find the default instance profile of your Elastic Beanstalk environment.

4.    Choose aws-elasticbeanstalk-ec2-role, and then choose Add inline policy.

5.    Choose the JSON view, and then enter the following code in the code editor: 

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "T2UnlimitedPermissions",
            "Effect": "Allow",
            "Action": "ec2:ModifyInstanceCreditSpecification",
            "Resource": "*"

        }
    ]
}

6.    Choose Review policy.

7.    For Name, enter a name for the new policy that you created.

8.    Choose Create policy.

Note: Your surplus credits are charged per vCPU-hour when you use T2 Unlimited instances.

Update your Elastic Beanstalk configuration file

1.    In the root of your application source bundle, create a directory named .ebextensions.

2.    In the .ebextensions directory, create a new file named 01-t2-unlimited.config, and then include the following: 

files:
  "/tmp/t2-test.sh":
      mode: "000755"
      content : |
        #!/bin/bash
        instanceType=$(curl -s http://169.254.169.254/latest/meta-data/instance-type)
        if [[ "$instanceType" =~ ^t2\. ]]
        then
          exit 0
        else
          exit 1
        fi

container_commands:
  set-t2-unlimited:
    command: |
      aws ec2 modify-instance-credit-specification --region $(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/[a-z]$//') --instance-credit-specification '[{"InstanceId": "'"$(curl -s http://169.254.169.254/latest/meta-data/instance-id)"'","CpuCredits": "unlimited"}]'
    test: "[ /tmp/t2-test.sh ]"

3.    To create an application source bundle with the updated configuration file from step 2, enter the following command from the root of the application source bundle:

zip ../myapp.zip -r * .[^.]*

Deploy your updated application to Elastic Beanstalk

1.    (Important) Verify that the inline policy created earlier is part of your Elastic Beanstalk instance profile. The inline policy grants EC2 instances in your Elastic Beanstalk environment permission to execute modify-instance-credit-specification. The .ebextension that's now included in the application bundle allows modify-instance-credit-specification to be executed during deployments.

2.    In the Elastic Beanstalk console, Elastic Beanstalk Command Line Interface, or AWS Command Line Interface, deploy your application source bundle to your Elastic Beanstalk environment.


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2019-03-29