Terraform 5 of 10: Resources Attributes and Dependencies using AWS Cloud9

Background

This tutorial deals with Terraform resource attributes and dependencies. AWS Cloud9 is the environment.

1 of 4. Open the Terraform documentation for "aws_instance" and "aws_security_group"

Terraform Registry > Providers > AWS > Documentation > EC2 (Elastic Compute Cloud) > aws_instance

Terraform Registry > Providers > AWS > Documentation > VPC (Virtual Private Cloud) > aws_security_group

Resource: aws_instance

Resource: aws_security_group

2 of 4. Open your AWS Cloud9 environment

Creating An Integrated Developer Environment (IDE) in the Cloud in Two Minutes!: AWS Cloud9(Step-by-Step)

3 of 4. Update "resources.tf" and "outputs.tf"

"outputs.tf" will be empty. The contents of "resources.tf" are below. We are setting up an instance, and referring to the security group dependency. If you're wondering why you would empty outputs.tf, notice that it refers to S3 bucket, but we don't have any S3 bucket resources. Run "terraform plan" and "terraform apply". Please note the order in which the resources were created, where the dependecy (security group) was created first, and THEN the instance was created. If you check the console versus your terminal, the ID of the resources created should match.

resource "aws_instance" "example" { ami = "ami-079db87dc4c10ac91" # This is an Amazon Linux 2023 AMI ID in the us-east-1 region instance_type = "t2.micro" vpc_security_group_ids = [aws_security_group.example.id] } resource "aws_security_group" "example" { name = "example" description = "Example security group" ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } }

4 of 4. Cleanup

Destroy the created resources. If you re-check your EC2 console, only your Cloud9 instance should be running.

terraform destroy

Reference

Create resource dependencies

Resource: aws_instance

Resource: aws_security_group

Comments

Popular posts from this blog

Orphaned No More: Adopting AWS Lambda

Containing the Chaos! | A Three-Part Series Demonstrating the Usefulness of Containerization to HumanGov