Search code examples
amazon-web-servicesautomationyamlcloudaws-cloudformation

How to reference existing resources in CloudFormation yaml template?


I want to only create the EC2 instance for automation purposes but to point to existing VPC, Subnet, Security groups and Internet Gateway.

Does anyone know what would it look like in the template file?

My current template looks like this. It fails when creating the stack and the instance is deleted automatically.

The error I received is CREATE_FAILED with the description "No default VPC for this user. GroupName is only supported for EC2-Classic and default VPC."

AWSTemplateFormatVersion: 2010-09-09
Parameters: 
  VPCId: 
    Type: AWS::EC2::VPC::Id
Resources:
  MySubnet:
    Type: AWS::EC2::Subnet
    Properties: 
      VpcId: !Ref VPCId
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-04ff9e9b51c1f62ca
      InstanceType: c5.xlarge
      KeyName: CloudFormation 
      SecurityGroupIds:
        - mySecurityGroup

Edited with template


Solution

  • SecurityGroupIds should be ID, not name. The security group IDs have format of sg-xxxxxxxxx, and that's what you have to use.