7

I am creating an Elastic Beanstalk environment using Cloudformation. I have to create an ApplicationVersion just to get it started and feed it into the definition of the environment. I create other ApplicationVersions and deploy them to the cluster in other ways (CodePipeline).

Now, every time I need to update the Cloudformation stack to change some other infrastructure, even though it doesn't list that as a potential resource change, it rolls back the ApplicationVersion to the initial one, and I'm having to manually update the environment to the latest version again.

I know what's going on - Cloudformation is just trying to keep the stack as the template describes it. I only ever defined the initial ApplicationVersion because it's a requirement for the Beanstalk environment. Is there any other way?

3 Answers 3

5

CloudFormation wants to be in control. Depending on the stack updates you perform, CloudFormation will re-create the version according to what's defined in the template.

Instead of deploying your version from Code Pipeline directly to Elastic Beanstalk, do the following:

  1. Don't hard code the initial version into your CloudFormation template.
  2. Have the version being deployed hooked to an input parameter to your CloudFormation stack. For example, have a input parameter be the version build number, and construct in your template a URL to that as your version source.
  3. When you deploy, instruct Code Pipeline to update your stack with the updated build number. CloudFormation should take over by building a new URL and deploying the version.

An Example:

Assuming you have a parameter ZipBucket and ZipObject in your stack, you can do the following on your AWS::ElasticBeanstalk::ApplicationVersion resource:

"SourceBundle"    : {
    "S3Bucket" : {
        "Ref" : "ZipBucket"
    },
    "S3Key"    : {
        "Ref" : "ZipObject"
    }
}

Another option is to have a BuildNumber parameter, and then use Fn::Join in the S3Key property to build a URL out of the build number.

12
  • Parameters! Didn't think of those, they can be useful. But what do I do to create the stack in the first place, when the initial application version doesn't exist yet? I create the application itself in CloudFormation too, same as application version.
    – snetch
    Feb 14, 2018 at 18:17
  • 1
    Just have a "hello world" version sitting in S3 if you don't have an actual build of your application ready. Feb 14, 2018 at 18:28
  • Which is pretty much what I have, I create it in the AWS::ElasticBeanstalk::ApplicationVersion resource, and I'm having to reference it in the AWS::ElasticBeanstalk::Environment resource. Actually I'm not sure how to parameterize that.
    – snetch
    Feb 14, 2018 at 18:34
  • See my example. Feb 14, 2018 at 19:09
  • Hey I have a question about this. When you update the application version in the cf template and deploy, does cf delete the old version or leave the previous one? I tried to deploy EB with terraform but it didn't work right because terraform would delete the previous app version before deploying the new one- maybe cf doesn't have this problem?
    – red888
    Sep 4, 2018 at 19:03
0

I have been playing around with Elastic Beanstalk, CodePipeline and CloudFormation and worked out a way to achieve something similar to what you desire (I think).

I used the CloudFormation CLI (create-stack) and a single template from the command line to create:

  • An initial Elastic Beanstalk application and environment using a "Hello, World" application that I had previously upload to an S3 bucket
  • A web hook for GitHub integration
  • A CodePipeline continuous delivery pipeline

The stack creation was successful and I initially had my "Hello, World" application running on Elastic Beanstalk. I was then able to deploy my actual application via the GitHub web hook and CodePipeline which overwrote the placeholder app.

I was worried that when I made changes to the environment (again using the CloudFormation CLI, this time with create-change-set and execute-change-set) I was going to redeploy the "Hello, World" app and overwrite my real one but this wasn't the case. My GitHub-sourced app was still the one deployed after applying the change set. Note that changes to AWS::ElasticBeanstalk::ApplicationVersion will result in a new application being deployed on Elastic Beanstalk and the real version being overwritten.

It's an imperfect solution and I am not sure why AWS has designed the Elastic Beanstalk-CloudFormation-CodePipeline integration as such and it feels weird having to first deploy a dummy application before the real thing. I have experienced similar headaches with Lambdas, so I am guessing it is by design rather than oversight.

0

The answers given above seem to be incorrect - or perhaps something has changed in AWS since they were written.

In my experience, any change to the SourceBundle in AWS::ElasticBeanstalk::ApplicationVersion (whether the resource is declared separately or as part of the ApplicationVersions parameter of AWS::ElasticBeanstalk::Application) will result in an error executing the changeset. The error I am getting is "You cannot update ApplicationVersions." It does not seem to matter whether I change the description or not.

The only solution I have been able to come up with thus far is to point my SourceBundle to a url that will not change. For example, develop for the development environment, main for production. I just need to make sure my latest code is deployed there before I issue any change set that could cause the version to be re-deployed.

If I'm missing something here I would love to learn if my approach could be improved.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.