Skip to content

Commit

Permalink
chore(aws-ec2): Fix code sample in docs for attaching volumes to EC2 (#…
Browse files Browse the repository at this point in the history
…14376)

Fix for #14111
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
stijnbrouwers authored Apr 26, 2021
1 parent 55e737f commit bf51f72
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/@aws-cdk/aws-ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -962,8 +962,12 @@ const instance = new ec2.Instance(this, 'Instance', {
volume.grantAttachVolumeByResourceTag(instance.grantPrincipal, [instance]);
const targetDevice = '/dev/xvdz';
instance.userData.addCommands(
// Retrieve token for accessing EC2 instance metadata (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html)
`TOKEN=$(curl -SsfX PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")`,
// Retrieve the instance Id of the current EC2 instance
`INSTANCE_ID=$(curl -SsfH "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id)`,
// Attach the volume to /dev/xvdz
`aws --region ${Stack.of(this).region} ec2 attach-volume --volume-id ${volume.volumeId} --instance-id ${instance.instanceId} --device ${targetDevice}`,
`aws --region ${Stack.of(this).region} ec2 attach-volume --volume-id ${volume.volumeId} --instance-id $INSTANCE_ID --device ${targetDevice}`,
// Wait until the volume has attached
`while ! test -e ${targetDevice}; do sleep 1; done`
// The volume will now be mounted. You may have to add additional code to format the volume if it has not been prepared.
Expand Down

0 comments on commit bf51f72

Please sign in to comment.