How to upgrade to the latest AMI in AWS Elastic Beanstalk

amazon-web-serviceselastic-beanstalk

I'm running an application on AWS Elastic Beanstalk, and it looks like I need to create a new environment if I want to use the latest AMI.

If I knew what the latest AMI id was, I could update it in the environment configuration.

Is there a place where I could find the ID of the latest Elastic Beanstalk AMIs, or even better, is it possible to have the instances automatically replaced by the new version whenever a new version is rolled out?

Best Answer

The recommended and supported way to upgrade your AWS Beanstalk environment is documented here and managed platform updates are discussed here, honestly I'd stick to that if you want things to be easy (and that's what Beanstalk is all about), you'll theoretically only get the non-breaking updates and AWS will manage the process so there's no downtime.

So I just want to reiterate that managed platform updates are probably what you or anyone else coming here from Google will want, but if you want to know the latest AWS provided AMI for your Beanstalk environment it can be done fairly trivially with AWS CLI (thanks to sane naming conventions from Amazon on their AMIs).

Starting with an instance from your environment, describe the instance to get the current AMI (skip if you already know the current AMI).

aws ec2 describe-instances --instance-ids i-0909613f35ec0ffee --query 'Reservations[*].Instances[*].ImageId' --output text

ami-35290a56

Take the resulting AMI ID and describe it.

aws ec2 describe-images --image-ids ami-35290a56 --query 'Images[*][Architecture, Hypervisor, Name, RootDeviceType, VirtualizationType]' --output json

[
    [
        "x86_64",
        "xen",
        "aws-elasticbeanstalk-amzn-2016.03.0.x86_64-python34-hvm-201603290718",
        "ebs",
        "hvm"
    ]
]

We can use the output of the above as input to a new, sorted describe-images but this time we replace the timestamps with * wildcard symbols, like so:

aws ec2 describe-images --filters 'Name=architecture,Values=x86_64' 'Name=virtualization-type,Values=hvm' 'Name=owner-alias,Values=amazon' 'Name=name,Values=aws-elasticbeanstalk-amzn-*.x86_64-python34-hvm-*' --query 'sort_by(Images[*], &Name)[-1].ImageId' --output text

ami-1be5de78

Due to the power of lexical sorting and ISO 8601, we end up with the latest AMI, which in my example is ami-1be5de78.

aws ec2 describe-images --image-ids ami-1be5de78 --query 'Images[*].Name' --output text

aws-elasticbeanstalk-amzn-2016.09.0.x86_64-python34-hvm-201612200708

Again, I wouldn't recommend you try to change to this AMI by hand, Beanstalk has provisions to do all this for you!