Security Group referencing in cloudformation is not working

0

I have 2 templates

  1. For security group. Resources: MyInstanceSecurityGroup: Type: 'AWS::EC2::SecurityGroup' Properties: GroupDescription: Enable SSH access to the instance SecurityGroupIngress: - IpProtocol: tcp FromPort: '22' ToPort: '22' CidrIp: '0.0.0.0/0' Outputs: MyInstanceSecurityGroup: Description: security GroupId Value: !GetAtt - MyInstanceSecurityGroup - GroupId Export: Name: !Sub "${AWS::StackName}-SecurityGroupID"

  2. Creating a web server using the security group in 1st stack


Parameters: Key: Description: Enter the name of the existing key pair Type: 'AWS::EC2::KeyPair::KeyName' ConstraintDescription: must be the name of an existing EC2 KeyPair. InstanceType: Description: WebServer EC2 instance type Type: String Default: t2.small AllowedValues: - t1.micro - t2.nano - t2.micro - t2.small ConstraintDescription: must be a valid EC2 instance type. Resources: MyElasticIP: Type: AWS::EC2::EIP Properties: InstanceId: !Ref MyInstance MyInstance: Type: AWS::EC2::Instance Properties: ImageId: ami-026b57f3c383c2eec AvailabilityZone: us-east-1a InstanceType: !Ref InstanceType KeyName: !Ref Key SecurityGroups: Fn::ImportValue !Sub "${sg-new}-MyInstanceSecurityGroup" But I am getting an error while creating a second stack as "Value of property SecurityGroups must be of type List of String" Could you help me resolve this?

bhawna
asked 9 months ago215 views
1 Answer
0

You export with a name like this;

Name: !Sub "${AWS::StackName}-SecurityGroupID"

But you import it with name like this:

Fn::ImportValue !Sub "${sg-new}-MyInstanceSecurityGroup

I'm not sure where ${sg-new} comes from, but the last part should match the name you exported, -SecurityGroupID, not the logical name from the first template.

profile pictureAWS
EXPERT
kentrad
answered 9 months ago
profile picture
EXPERT
reviewed 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions