Lab 03: Intrinsic Functions

Overview

This lab shows how to use Intrinsic Functions in your template.

Intrinsic functions are built-in functions that help you manage your stacks. Without them, you will be limited to very basic templates, similar to the S3 template you have seen in a Lab01.

Topics Covered

In this Lab, you will:

  • Use the Fn::Ref function to dynamically assign parameter values to a resource property.
  • Tag an instance with Fn::Join function.
  • Add a tag to the instance using Fn::Sub function.

Start Lab

  1. Go to the code/20-cloudformation-features/ directory.
  2. Open the 03-lab03-IntrinsicFunctions.yaml file.
  3. Copy the code as you go through the topics below.

Intrinsic functions can only be used in certain parts of a template. You can use intrinsic functions in resource properties, outputs, metadata attributes, and update policy attributes.

Fn::Ref

In the last lab you have “hard coded” an AMI ID directly into the EC2 Resource property. You will now amend this to make your template more flexible. Let’s convert AmiID to variable and pass it to resource property at the runtime.

  1. First, create new parameter called AmiID and put it in the Parameters section of your template.

     AmiID:
       Type: AWS::EC2::Image::Id
       Description: 'The ID of the AMI.'
    
  2. Use the intrinsic function Ref to pass the AmiID parameter input to the EC2 resource property.

    Resources: WebServerInstance: Type: AWS::EC2::Instance Properties: # Use !Ref function in ImageId property ImageId: !Ref AmiID InstanceType: !Ref InstanceType

Fn::Join

To help you manage your AWS resources, you can optionally assign your own metadata to each resource in the form of tags. Each tag is a simple label consisting of a customer-defined key and an optional value that can help you to categorize resources by purpose, owner, environment, or other criteria. Let’s use the intrinsic function Fn::Join to name your instance.

  1. Add property Tags to the Properties section.
  2. Reference InstanceType parameter and add a word webserver, delimited with dash - to the tags property.

    Resources: WebServerInstance: Type: AWS::EC2::Instance Properties: ImageId: !Ref AmiID InstanceType: !Ref InstanceType Tags: - Key: Name Value: !Join [ ‘-’, [ !Ref InstanceType, webserver ] ]

Update EC2 stack

Now it is time to update your stack. Go to the AWS console and update your CloudFormation Stack.

  1. Open the AWS CloudFormation link in a new tab and log in to your AWS account.
  2. Click on the stack name, for example cfn-workshop-ec2.
  3. In the top right corner click on Update.
  4. In Prepare template, choose Replace current template.
  5. In Template source, choose Upload a template file.
  6. Click on Choose file button and navigate to your workshop directory.
  7. Select the file 03-lab03-IntrinsicFunctions.yaml and click Next.
  8. For Type of EC2 Instance leave the default value in.
  9. For Amazon Machine Image ID copy and paste AMI ID you have hardcoded in 01-lab02-Resources.yaml file and click Next.
  10. You can leave Configure stack options default, click Next.
  11. On the Review page, scroll down to the bottom and click on Update stack.
  12. You can click the refresh button a few times until you see in the status UPDATE_COMPLETE.
Want to see how to update the stack?

To see the result of the stack update:

  1. Open AWS EC2 console link in a new tab of your browser.
  2. In the left hand pane, click on Instances.
  3. Select the instance with a name t2.micro-webserver
  4. Go to the Tags tab, you should see there a key Name with a value t2.micro-webserver.

    tags-png

Challenge

Crete another tag named InstanceType and use intrinsic function Fn::Sub to return type of the instance.

The syntax for the short form is !Sub

Need a hint?
Want to see the solution?

Conclusion

Congratulations! You now have successfully used intrinsic functions in your template.