I'm trying to create an EC2 instance in a VPC using cloudformation, when I run the following:
aws cloudformation create-stack --stack-name stack --region us-east-1 --template-body file://file.yml
I get the following error:
An error occurred (ValidationError) when calling the CreateStack operation:
Template format error: Unresolved resource dependencies [subnet-057ba3df40f87da4e]
in the Resources block of the template
known that this is the yaml file I'm using to create this stack:
Resources:
accessSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: "new EC2 security group for HTTP and SHH ports"
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- IpProtocol: -1
FromPort: -1
ToPort: -1
CidrIp: 0.0.0.0/0
VpcId: "vpc-09495a820716bff3b"
EC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: "ami-0022f774911c1d690"
InstanceType: t3.micro
NetworkInterfaces:
- AssociatePublicIpAddress: "true"
DeviceIndex: "0"
GroupSet:
- Ref: "accessSecurityGroup"
SubnetId:
Ref: "subnet-057ba3df40f87da4e"
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
SecurityGroupIds:
- !Ref accessSecurityGroup
I'm positive that the above VpcId
, and SubnetId
exist, and I'm not sure what seems to be the problem.
CodePudding user response:
The intrinsic function
Ref returns the value of the specified parameter or resource.
Drop Ref
since it's not referring to a parameter or resource, and the value is hardcoded
SubnetId: "subnet-057ba3df40f87da4e"
Recommend trying the CloudFormation Linter in VSCode to see these errors inline while authoring templates along with autocompletion and documentation links:
E1012 Ref subnet-057ba3df40f87da4e not found as a resource or parameter