0
votes

How to create more than one environment under a single Elasticbeanstalk application, using cloudformation.

I have a created a cloudformation template which basically creates a CI/CD pipeline on AWS by provisioning necessary resources. I have a problem when it comes to creating multiple environments(e.g staging and prod) under same elastic beanstalk application using the template. When I deploy the template for the first time, say a staging environment, it works perfectly, but when I redeploy it in this case for production environment, it fails with the error that the application name already exists. I have tried to use cloudformation conditions but wasn't successful. I was wondering if there's a way I can have a condition to check if a similar ApplicationName exist, it skips creation of the application name and creates an environment under the name.

    WebApplication:
        Type: AWS::ElasticBeanstalk::Application
        Properties:
          ApplicationName: !Sub "${GithubRepo}"
          Description: "Application Description"

      WebApplicationEnvironment:
        Type: AWS::ElasticBeanstalk::Environment
        Properties:
          ApplicationName: !Ref WebApplication
          EnvironmentName: !Sub "${GithubRepo}-${Stage}"
          TemplateName: !Ref WebApplicationTemplate

I expect when I create when I run the template the second time, a new environment should be created in the specified application.

1

1 Answers

0
votes

I was wondering if there's a way I can have a condition to check if a similar ApplicationName exist, it skips creation of the application name and creates an environment under the name.

Short answer is yes, if you're willing to build a custom resource that can check and return a value that you can use for a conditional in your WebApplication. But it's a terrible idea, and here's why: your stage stack's resources will also be used in prod, but since you are implicitly coupling them between stacks, cloudformation won't know how to handle updates.

if you want a one-to-many relationship between WebApplication and WebApplicationEnvironment, it might be best to create them in diffrent stacks. You can use Stack Exports to expose one stack's data to another stack. The Stack exports will protect you from making unintentional changes to resources that other stacks depend on.

But a better approach for you would probably be to create two complete distinct stacks for each environment. Otherwise, you won't be able to test the prod upgrade process (when you upgrade stage, you'll upgrade prod).

If you're really trying to cut costs, then put all the environments in the same stack. If you can't use them to keep different environments distinct anyway, better to share a stack than implicitly share resources between stacks.