I’m trying to create a CloudFormation template that has, among other things, an EC2 Instance with an attached EBS volume. The user data in the EC2 instance should mount that volume and write some data into it:
Resources:
#More resources in the real template
EBSVolume:
Type: AWS::EC2::Volume
Properties:
AvailabilityZone: !GetAtt Subnet.AvailabilityZone
VolumeType: gp3
Size: 10
EBSVolumeMount:
Type: AWS::EC2::VolumeAttachment
Properties:
Device: /dev/sdf
InstanceId: !Ref Instance
VolumeId: !Ref EBSVolume
Instance:
Type: AWS::EC2::Instance
CreationPolicy:
ResourceSignal:
Timeout: PT5M
Properties:
InstanceType: t2.micro
ImageId: !Ref LatestAmiId
SubnetId: !Ref Subnet
SecurityGroupIds:
- !Ref InstanceSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
yum -y update
yum -y upgrade
yum install -y aws-cfn-bootstrap
#Create the file system for the EBS volume and mount it
mkfs -t xfs /dev/xvdf
mkdir /ebs
mount /dev/xvdf /ebs
blkid -s UUID -o value /dev/xvdf | xargs -I {} sudo sh -c "echo 'UUID={} /ebs xfs defaults,nofail 0 2' >> /etc/fstab"
(more bootstrapping that involves writing data into /ebs)
#Signal CloudFormation
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource Instance --region ${AWS::Region}
The problem that I’m encountering with the above setup is that the AWS::EC2::VolumeAttachment
resource depends on the AWS::EC2::Instance
resource but its user data expects the volume to be created. If it isn’t, the script fails and the CFN signal is never sent, therefore failing the stack.
If I don’t use cfn-signal
it works because the volume is provisioned and available before the user data is run as the CREATE_COMPLETE signal is sent before running the user data. Is there a way for me to use cfn-signal
in this scenario?