Using Terraform Dynamic Blocks and Built-in Functions to Deploy to AWS

preview_player
Показать описание
Introduction
Terraform offers a strong set of features to help optimize your Terraform code. Two really useful features are dynamic blocks, which allow you to generate static repeated blocks within resources in Terraform; and built-in functions, which help you manipulate variables and data to suit your needs and help make your Terraform deployments better automated and more fault resilient.

Solution
Log in to the lab server using the credentials provided:

In a web browser, log in to the AWS Management Console using the credentials provided.

Check Terraform Status
Check the Terraform status using the version command:

terraform version

Note: If you receive a notification that there is a newer version of Terraform available, you can ignore it — the lab will run safely with the version installed on the VM.

Clone Terraform Code and Switch to Proper Directory
The Terraform code required for this lab has already been cloned onto the provided VM. Switch to the directory where the code is located:

cd lab_code
cd section7-HoL-TF-DynBlocks-Funcs
ls

Examine the Code in the Files

Closely examine the code and note the following:

We have selected AWS as our provider and our resources will be deployed in the us-east-1 region.
We are using the ssm_parameter public endpoint resource to get the AMI ID of the Amazon Linux 2 image that will spin up the EC2 webserver.
We are using the vpc module (provided by the Terraform Public Registry) to create our network components like subnets, internet gateway, and route tables.
We are also using a couple of built-in functions and some logical expressions in the code to get it to work the way we want, including the join function for the name attribute in the security group resource, and the fileexists and file functions for the user_data parameter in the EC2 instance resource.
Enter q to exit the less program.

Enter q to exit the less program.

The Web-Server-URL output is the publicly accessible URL for our webserver. Notice here that we are using the join function for the value parameter to generate the URL for the webserver.
The Time-Date output is the timestamp when we executed our Terraform code.
Review and Deploy the Terraform Code:

terraform fmt
terraform init
terraform validate
terraform plan

Deploy the code:

terraform apply --auto-approve

Test Out the Deployment and Clean Up
Once the code has executed successfully, view the outputs at the end of the completion message:

The Time-Date output displays the timestamp when the code was executed.
The Web-Server-URL output displays the web address for the Apache webserver we created during deployment.

terraform output

Verify that the resources were created correctly in the AWS Management Console:

Navigate to the AWS Management Console in your browser.
Type VPC in the search bar and select VPC from the contextual menu.
Verify that the my-vpc resource appears in the list.
Type EC2 in the search bar and select EC2 from the contextual menu.
Verify that the instance, which has no name, appears in the list (and is likely still initializing).
In the menu on the left, click Security Groups.
Verify that the Terraform-Dynamic-SG security group appears in the list.
Select the security group to see further details.
Click on the Inbound rules tab, and note that three separate rules were created from the single dynamic block used on the ingress parameter in the code.

In the CLI, copy the URL displayed as the Web-Server_URL output value.

In a new browser window or tab, paste the URL and press Enter.

Verify that the Apache Test Page loads.

terraform destroy --auto-approve
Рекомендации по теме