0% found this document useful (0 votes)
5 views

Lab Activity

The document outlines a series of lab activities for setting up and managing AWS resources using Terraform in a virtual environment. It includes detailed steps for creating S3 buckets, EC2 instances, VPCs, and managing Terraform configurations, including backend setups and versioning. Additionally, it provides instructions for using Terraform Cloud for organization and workspace management.

Uploaded by

Prachi desai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab Activity

The document outlines a series of lab activities for setting up and managing AWS resources using Terraform in a virtual environment. It includes detailed steps for creating S3 buckets, EC2 instances, VPCs, and managing Terraform configurations, including backend setups and versioning. Additionally, it provides instructions for using Terraform Cloud for organization and workspace management.

Uploaded by

Prachi desai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Lab Activity 1:

Note: Follow these steps to create a base environment

Note: I am using virtual environment (Ubuntu VM) to work with Terraform

1. Download or install the terraform application from the link below:


a. https://www.terraform.io/downloads.html

2. Copy and paste or type the commands to install terraform.


3. Once you install terraform in your system, base environment is done. No need to do these steps
all the time if you are using VMs.
4. Follow the same steps for Demo1, Demo2, Demo3 and Demo6. Only the change will be in the
configuration file.
5. Create a directory, using mkdir command.
a. Suppose mkdir s3.
6. Create one file inside it. Use touch main.tf command. (main is my file name, you can
choose yours but make sure the file should have tf extension)

7. Put the following content in the file.


provider "aws" {
access_key = "AKIAWLISMY5OXIWKTXQ6"
secret_key = "EUk0Dy5kkCufuvVQQnt8OWBK9VoGsAyA5Z9+NpXs"
region = "us-west-2"
}

resource "aws_s3_bucket" "example" {


bucket = "testing-env-2023"
}
8. Your access key and secret key will be different. So, do not forget to edit it. Use unique name for
the bucket.
9. Run terraform init command.
10. Run terraform plan command.

11. Run terraform apply command.


12. Check S3 dashboard. You can see the bucket is created.

13. For the second exercise, you need to add a backend block in the existing configuration file
14. Edit the file using nano main.tf command.

Note: Change the highlighted content accordingly


15. Add the following content:
provider "aws" {
access_key = "AKIAWLISMY5ORMPD6FGP"
secret_key = "6gw3zGb7IIGmtRM7cTDFVmVkgwtIrQTOJevE97W/"
region = "us-west-2"
}

resource "aws_s3_bucket" "example" {


bucket = "testing-env-2023"
}
terraform {
backend "s3" {
bucket = "testing-env-2023"
key = "terraform.tfstate-s3"
region = "us-west-2"
encrypt = false
dynamodb_table = "terraform-lock-table"
}
}
16. Run the following command
a. terraform init -backend-
config="access_key=AKIAWLISMY5ORMPD6FGP" -backend-
config="secret_key=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"
"-lock=false"
17. You can see terraform.tfstate-s3 file is been created in S3 bucket
18. Run the terraform destroy “-lock=false” command to delete the created
resources.
Lab Activity 2:

Note: Follow these steps to create a base environment

Note: I am using a virtual environment (Ubuntu VM) to work with Terraform

1. Download or install the Terraform application from the link below:


a. https://www.terraform.io/downloads.html

2. Copy and paste or type the commands to install terraform.


3. Once you install terraform in your system, base environment is done. No need to do these steps
all the time if you are using VMs.
4. Follow the same steps for Demo1, Demo2, Demo3 and Demo6. Only the change will be in the
configuration file.
5. Create a directory, using mkdir command.
a. Suppose mkdir s3.
6. Create one file inside it. Use touch main.tf command. (main is my file name, you can
choose yours but make sure the file should have tf extension)

7. Put the following content in the file.


provider "aws" {
access_key = "AKIAWLISMY5OXIWKTXQ6"
secret_key = "EUk0Dy5kkCufuvVQQnt8OWBK9VoGsAyA5Z9+NpXs"
region = "us-west-2"
}

resource "aws_s3_bucket" "example" {


bucket = "testing-env-2023"
}
8. Your access key and secret key will be different. So, do not forget to edit it. Use unique name for
the bucket.
9. Run terraform init command.
10. Run terraform plan command.
11. Run terraform apply command.
12. Check S3 dashboard. You can see the bucket is created.

13. Go to the properties tab of the bucket, scroll down and check the versioning feature, it is in
disable mode now.
14. Go to the Terraform configuration file and edit it. Add the following data (versioning part only)
provider "aws" {
access_key = "AKIAWLISMY5ORMPD6FGP"
secret_key = "6gw3zGb7IIGmtRM7cTDFVmVkgwtIrQTOJevE97W/"
region = "us-west-2"
}
resource "aws_s3_bucket" "example" {
bucket = "testing-env-2023"
versioning {
enabled = true
}
}
15. Run the terraform plan and terraform apply commands one after another.
16. After Terraform apply command, Check the properties tab of S3 bucket in the AWS
console you can see the versioning is enable now.
17. Do not forget to run the terraform destroy command.
Lab Activity 3:
1. Create a directory using mkdir ec2_inst command.
2. Create 3 files in it main.tf, variable.tf and output.tf

3. Contents of main.tf

resource "aws_instance" "ec2_instance" {

ami = var.ami_id

instance_type = var.instance_type

subnet_id = var.subnet_id

# Add any other desired instance configurations here

# Add any other resources required for your use case (e.g., security groups, key pairs, etc.)

4. Contents of variables.tf

variable "ami_id" {

description = "The ID of the AMI to use for the EC2 instance"

variable "instance_type" {

description = "The type of EC2 instance to launch"

variable "subnet_id" {

description = "The ID of the subnet in which to launch the EC2 instance"


}

# Add any other variables required for your use case

5. Contents of outputs.tf

output "ec2_instance_id" {

value = aws_instance.ec2_instance.id

description = "The ID of the launched EC2 instance"

# Add any other outputs you want to expose

6. Create one more directory in any location. Use mkdir command.


7. Create one file inside it lets say main.tf
8. Content of main.tf are as follows:

provider "aws" {

access_key = "AKIAWLISMY5OXIWKTXQ6"

secret_key = "EUk0Dy5kkCufuvVQQnt8OWBK9VoGsAyA5Z9+NpXs"

region = "us-west-2"

module "my_ec2_instance" {

source = "/root/ec2_inst" #Use your source path

ami_id = "ami-04e914639d0cca79a"

instance_type = "t2.micro"

# key_name = "my-key-pair"

subnet_id = "subnet-0045214a773d5e406"

9. Run the same commands to deploy ec2 using terraform.


a. terraform init
b. terraform plan
c. terraform apply
10. Check EC2 dashboard:
11. Do not forget to run terraform destroy command.
Lab Activity 4:
1. Follow the same procedure as we have done in the previous exercises.
2. Put the code in main.tf file change your values accordingly

provider "aws" {

access_key = "AKIAWLISMY5ORMPD6FGP"

secret_key = "6gw3zGb7IIGmtRM7cTDFVmVkgwtIrQTOJevE97W/"

region = "us-west-2" # Update with your desired region

# Create VPC

resource "aws_vpc" "my_vpc" {

cidr_block = "10.0.0.0/16" # Update with your desired CIDR block

tags = {

Name = "MyVPC"

# Create an Internet Gateway (IGW) and attach it to the VPC

resource "aws_internet_gateway" "my_igw" {

vpc_id = aws_vpc.my_vpc.id

tags = {

Name = "MyIGW"

# Create a public subnet within the VPC

resource "aws_subnet" "public_subnet" {


vpc_id = aws_vpc.my_vpc.id

cidr_block = "10.0.1.0/24" # Update with your desired CIDR block

availability_zone = "us-west-2a" # Update with your desired availability zone

tags = {

Name = "PublicSubnet"

# Create a route table for the public subnet

resource "aws_route_table" "public_route_table" {

vpc_id = aws_vpc.my_vpc.id

route {

cidr_block = "0.0.0.0/0"

gateway_id = aws_internet_gateway.my_igw.id

tags = {

Name = "PublicRouteTable"

# Associate the public subnet with the public route table

resource "aws_route_table_association" "public_subnet_association" {

subnet_id = aws_subnet.public_subnet.id

route_table_id = aws_route_table.public_route_table.id

}
# Output the VPC ID and Subnet ID

output "vpc_id" {

value = aws_vpc.my_vpc.id

output "public_subnet_id" {

value = aws_subnet.public_subnet.id

3. Once everything is done, Run:


a. terraform init
b. terraform plan -out tfplan (here we are saving our plan in tfplan file)

c. terraform apply tfplan command

Final Output:
VPC Dashboard:

4. Now, if the administrator does not want to delete VPC, but want to delete other components
like IGW, subnet, route table etc. let’s see how to do it.
5. Run terraform state list command
6. Copy the my_vpc resource name from the list and run terraform state rm
aws_vpc.my_vpc command

7. Run the terraform destroy command. Review the plan before answering Yes.
8. The plan will show the deletion of other components:

9. Except VPC everything gets deleted.

Terraform Workspace:

1. Visit the Terraform Cloud website (https://app.terraform.io/) in your web browser.

2. Provide your details and click on create account. Server will send you a verification link on the
email you have provided. Click on it to verify your mail ID.
3. When you click the link to confirm your email address, the Terraform Cloud UI will ask which
setup workflow you would like use. Select Start from scratch.
4. Create an Organization: Creating organizations of up to 5 users is free. Provide the name and
email address then click on Create organization button.

5. Create a new workspace. Select CLI-driven workflow. provide a name for the workspace and
click on create workspace.
6. Your workspace on Terraform cloud is been created. You can use example code in your code file
to use this workspace.

You might also like