Randal
Randal

Reputation: 431

ec2 cloudformation template without default vpc

The EC2 template is a basic template but it fails because the default vpc no longer exist. As a result it errors, Usually ill set it up under Paremeters, Default value.

creating a default VPC is unfortunately not an option....

How can this be modified to use a particular VPC?

only after figuring out what the errors really mean I have learned that I have to assign the VPC and can't tell it to use the 'default'

Parameters:
  VPC:
    Description: Testing using  VPC created
    Type: String
    Default: vpc-8787789
  Subnet:
    Type: String
    Default: subnet-7657657578



...
...
...
Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref 'InstanceType'
      VpcId: !Ref VPC
      SecurityGroups:
        - !Ref 'InstanceSecurityGroup'
      KeyName: !Ref 'KeyName'
      ImageId: !FindInMap
        - AWSRegionArch2AMI
        - !Ref 'AWS::Region'
        - HVM64
  InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Enable SSH access via port 22
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
          CidrIp: !Ref 'SSHLocation'

I guess what I am trying to understand is how I modify this code to use a specific vpcid.

Upvotes: 1

Views: 1445

Answers (2)

Madaditya
Madaditya

Reputation: 173

Add a subnet id to the EC2 config, VpcID to the security group and you should be able to create EC2 and linked security group with a non default VPC

  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      SubnetId: !Ref EC2SubnetID
    ...
  InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      Vpcid: !Ref VpcId
    ...

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 269101

The error is most probably coming from InstanceSecurityGroup because it is not specifying a value for VpcId. Therefore, it is defaulting to the Default VPC, which does not exist. To fix this, add: VpcId: VPC

  InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Enable SSH access via port 22
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
          CidrIp: !Ref 'SSHLocation'
      VpcId: !Ref VPC

Upvotes: 2

Related Questions