Terraform is an open source Infrastructure as Code (IAC) tool created by HashiCorp that enables users to provision an infrastructure or manage IAC. Terraform works by reading the code and translating it to API calls to AWS, Microsoft Azure, and Google Cloud Platform (GCP).
Creating resources in Terraform
We need to define the following in order to enable Terraform to know the credentials and authenticate as well as create resources:
Resource: This defines an infrastructure object like an EC2 instance or an S3 bucket.
Logical name: The logical name is used to refer to this resource from elsewhere in the Terraform script but it's scope is limited to the internal of the module.
Instance type: The type of EC2 instance to run, such as t2.micro. Varies in CPU, memory, I/O capabilities.
AMI: An operating system image to run EC2 instances.
Running a script written in Terraform
terraform init
terraform plan
terraform apply
terraform destroy if you need to delete it
Terraform scripts:
Create an EC2 instance
$ cat ec2-instance.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "test_instance" {
ami = "ami-038f1ca1bd58a5790"
instance_type = "t2.micro"
}
$
Define IAM role for a specific user
$ cat iam_role.tf
provider "aws" {
region = "us-west-2"
}
resource "aws_iam_role_policy" "my-test-policy" {
name = "my-test-iam-policy"
role = "${aws_iam_role.my-test-iam-role.id}"
policy = <<EOF
{
"Version": "2021-03-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:PutObject",
"s3:GetObject"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role" "my-test-iam-role" {
name = "my-test-iam-role"
assume_role_policy = <<EOF
{
"Version": "2021-03-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
tags = {
tag-key = "my-test-iam-role"
}
}
resource "aws_iam_instance_profile" "my-test-iam-instance-profile" {
name = "my-test-iam-instance-profile"
role = "${aws_iam_role.my-test-iam-role.name}"
}
resource "aws_instance" "test_ec2_role" {
ami = "ami-038f1ca1bd58a5790"
instance_type = "t2.micro"
iam_instance_profile = "${aws_iam_instance_profile.my-test-iam-instance-profile.name}"
key_name = "rahul-dravid"
}
$
Creating a Transit Gateway
$ cat transit-gateway.tf
provider "aws" {
region = "us-west-2"
}
data "aws_vpc" "vpc-dr" {
filter {
name = "tag:Name"
values = ["vpc-dr"]
}
}
data "aws_subnet_ids" "vpc-dr-subnet" {
vpc_id = data.aws_vpc.vpc-dr.id
}
data "aws_route_table" "vpc-dr-rt" {
filter {
name = "tag:Name"
values = ["vpc-dr-public-route-table"]
}
}
resource "aws_ec2_transit_gateway" "tgw-dr" {
description = "transit gateway for DR environment"
amazon_side_asn = 63363
auto_accept_shared_attachments = "disable"
default_route_table_association = "enable"
default_route_table_propagation = "enable"
dns_support = "enable"
vpn_ecmp_support = "enable"
tags = {
Name = "tgw-dr"
}
}
resource "aws_ec2_transit_gateway_vpc_attachment" "tgw-dr-attachment" {
transit_gateway_id = aws_ec2_transit_gateway.tgw-dr.id
vpc_id = data.aws_vpc.vpc-dr.id
dns_support = "enable"
subnet_ids = data.aws_subnet_ids.vpc-dr-subnet.ids
tags = {
Name = "tgw-dr-subnet"
}
}
resource "aws_route" "my-tgw-route" {
route_table_id = data.aws_route_table.vpc-dr-rt.id
destination_cidr_block = "10.133.72.0/23"
transit_gateway_id = aws_ec2_transit_gateway.tgw-dr.id
}
$
Creating a nightly instance
$ cat nightly_stop_instance.tf
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2021-7-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy" "lambda_logging" {
name = "lambda_logging"
path = "/"
description = "IAM policy for logging from a lambda"
policy = <<EOF
{
"Version": "2021-17-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:Start*",
"ec2:Stop*"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_logs" {
role = aws_iam_role.iam_for_lambda.name
policy_arn = aws_iam_policy.lambda_logging.arn
}
resource "aws_lambda_function" "test_lambda" {
filename = "lambda.zip"
function_name = "stop_ec2_nightly"
role = aws_iam_role.iam_for_lambda.arn
handler = "lambda.lambda_handler"
source_code_hash = base64sha256("lambda.zip")
runtime = "python3.7"
}
resource "aws_cloudwatch_event_rule" "cron_expr" {
name = "cron-expression"
description = "Fires every day at 7:45 pm UTC"
schedule_expression = "cron(45 19 * * ? *)"
}
resource "aws_cloudwatch_event_target" "cron_expr_target" {
rule = aws_cloudwatch_event_rule.cron_expr.name
target_id = "lambda"
arn = aws_lambda_function.test_lambda.arn
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_lambda" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.test_lambda.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.cron_expr.arn
}
$
No comments:
Post a Comment