-
Objective: Set up programmatic access to AWS by creating access keys.
-
Instructions:
- Go to the AWS IAM Console.
- Create a new IAM user with programmatic access only (no console access).
- Download the access key ID and secret access key for this user.
-
Expected Outcome:
- You have a new IAM user with an Access Key ID and Secret Access Key to use with AWS CLI and Terraform.
-
Objective: Configure a named AWS CLI profile to manage AWS resources.
-
Instructions:
-
Open your terminal and configure a new AWS CLI profile using the access keys from Exercise 1:
aws configure --profile develop
-
Enter the Access Key ID, Secret Access Key, and Default region name (e.g.,
us-east-1) when prompted.
-
-
Expected Outcome:
- A new AWS CLI profile named
developthat can be used to authenticate Terraform with AWS.
- A new AWS CLI profile named
-
Objective: Write a basic Terraform configuration to create an EC2 instance without using variables.
-
Instructions:
mkdir exercise-3- In a project directory, create a
main.tffile. - Define the AWS provider with
regionandprofilehardcoded. - Define an
aws_instanceresource withami,instance_type, andtagshardcoded.
-
Expected Outcome:
- An EC2 instance is created in AWS using Terraform, with static values for its AMI, instance type, and tags.
- Create the resource
-
Objective: Modify the
main.tfconfiguration to use variables instead of hardcoded values. -
Instructions:
mkdir exercise-4- Create a
variables.tffile to define the following variables:regionprofileamiinstance_typetags(as a map for Name and Environment)
- In
main.tf, replace hardcoded values with references to these variables. - Provide default values in
variables.tfforregion,profile, andinstance_type.
-
Expected Outcome:
- The EC2 instance is created using values from variables rather than hardcoded values, making the configuration more flexible.
-
Objective: Use environment-specific
.tfvarsfiles to manage different configurations. -
Instructions:
-
mkdir exercise-5 -
In the project directory, create a
dev.tfvarsfile. -
Set different values in
dev.tfvarsforami,instance_type, andtags. -
Run
terraform applywith thedev.tfvarsfile to apply the configuration.terraform apply -var-file="dev.tfvars"
-
-
Expected Outcome:
- The configuration applies with values specific to the
dev.tfvarsfile, showing how to manage environment-specific configurations.
- The configuration applies with values specific to the
-
Objective: Define a security group for SSH access in your Terraform configuration.
-
Instructions:
- In
main.tf, add anaws_security_groupresource with rules to allow SSH access (port 22). - Associate this security group with your EC2 instance by adding
vpc_security_group_idsin theaws_instanceresource.
- In
-
Expected Outcome:
- The EC2 instance is deployed with an attached security group that allows SSH access.
- Create the resource
-
Objective: Organize your Terraform configuration by creating a module for EC2 instances.
-
Instructions:
- In your project directory, create a
modulesfolder with anec2_instancesubfolder. - Move the EC2 instance configuration to
modules/ec2_instance/main.tf. - Define variables in
modules/ec2_instance/variables.tfforami,instance_type,tags, andsecurity_groups. - Reference this module in the root
main.tffile using thesourcekeyword, and pass the necessary values.
- In your project directory, create a
-
Expected Outcome:
- The EC2 instance is created using the module, and you now have a reusable EC2 module for future projects.
-
Objective: Add another module (e.g.,
security_group) to manage the EC2 instance’s security separately. -
Instructions:
- Create a
security_groupmodule in themodulesfolder. - Define a security group resource in
modules/security_group/main.tf. - Reference both
ec2_instanceandsecurity_groupmodules in the rootmain.tf, passing the security group output to the EC2 module.
- Create a
-
Expected Outcome:
- The EC2 instance is deployed with a security group created by a separate module, showing how to use multiple modules for modular and scalable Terraform configurations.
-
Objective: Use Terraform to destroy all resources created during the exercises.
-
Instructions:
-
In the project directory, run the following command to destroy all resources:
terraform destroy -var-file="dev.tfvars" -
Confirm the destroy operation when prompted.
-
-
Expected Outcome:
- All AWS resources created during the exercises are deleted, demonstrating how to manage infrastructure lifecycle with Terraform.