Home > Blockchain >  Reuse Elastic Beanstalk resource creating in one cloud formation stack in a second cloud formation s
Reuse Elastic Beanstalk resource creating in one cloud formation stack in a second cloud formation s

Time:11-06

I have an existing cloud formation stack which creates an Elastic Beanstalk app:

 Resources:
   EBApplication:
     Type: AWS::ElasticBeanstalk::Application
     Properties:
       ApplicationName: !Ref ApplicationName
       Description: "AWS Elastic Beanstalk Account Balance application"

I want to use this resource in another stack, so Im exporting it at the end of the same yaml file:

Outputs:
  EBApplicationName:
    Value: !Ref EBApplication
    Export:
      Name: card-balance-EBApplicationName

Now in my second cloud formation stack, I was to use the elastic beanstalk resource, Im trying:

Resources:
  EBApplication:
    Type: AWS::ElasticBeanstalk::Application
    Properties:
      ApplicationName: !ImportValue 
            'Fn::Sub': 'card-balance-EBApplicationName'

But I get an error saying the application name is already being used, as it's trying to create a new Elastic Beanstalk app (with same same) rather than reusing the first one. What am I doing wrong?

EDIT This question here has a similar problem. Essentially I want 2 separate stacks - one for each environment - but these are under the same EB application. It seem the person achieved this, see their comment under the answer How to create multiple Elasticbeanstalk environments using a single cloudformation template

CodePudding user response:

What am I doing wrong?

You can't "reusing the first one" the first one. All modifications to the first EB environment must be performed using the first CFN stack.

In the second stack you can only reference EB return values in other resources, once you export these values in the first stack.

CodePudding user response:

The EB Application resource must be in one and only stack:

EBApplication:
  Type: AWS::ElasticBeanstalk::Application
  Properties:
     ApplicationName: !Ref EBApplicationName
     Description: "Application Description"

In this stack, you can reference the application in an environment:

EBApplicationEnvironment1:
  Type: AWS::ElasticBeanstalk::Environment
  Properties:
    ApplicationName: !Ref EBApplication
    EnvironmentName: !Ref EBnvironmentName1

This is because, as per the documentation Ref returns the resource name.

In another stack you can't reference by resource, but you can by name:

EBApplicationEnvironment2:
  Type: AWS::ElasticBeanstalk::Environment
  Properties:
    ApplicationName: !Ref EBApplicationName
    EnvironmentName: !Ref EBnvironmentName2

With Ref you can reference both another resource (in the same stack only) and a parameter. So for your case, you could in both stacks pass the same application name as a parameter. You don't necessarily need to output the name. (This isn't always the case with CloudFormation. It works here, because you are allowed to define the name and you can reference the resource in the Environment by name. ARNs or ids are needed in other cases. An output will work there).

Also note that the first stack owns the application and environment 1. You will need to delete (when needed) first the second stack and then the first.

  • Related