AWS Service Resource Blocks in Terraform
1. Amazon S3 (Simple Storage Service)
Resource Block:
resource "aws_s3_bucket" "example" {
bucket = "my-example-bucket"
acl = "private"
tags = {
Name = "example-bucket"
Environment = "dev"
}
}
Notes:
bucket
specifies the name of the bucket.acl
sets the access control list for the bucket.
2. Amazon EC2 (Elastic Compute Cloud)
Resource Block:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
}
Notes:
ami
specifies the Amazon Machine Image ID.instance_type
defines the instance type (e.g.,t2.micro
).
3. Amazon RDS (Relational Database Service)
Resource Block:
resource "aws_db_instance" "example" {
identifier = "mydbinstance"
instance_class = "db.t3.micro"
engine = "mysql"
engine_version = "8.0"
username = "admin"
password = "password"
allocated_storage = 20
tags = {
Name = "example-db"
}
}
Notes:
instance_class
defines the instance type.engine
specifies the database engine (e.g.,mysql
).
4. Amazon ALB (Application Load Balancer)
Resource Block:
resource "aws_lb" "example" {
name = "example-alb"
internal = false
load_balancer_type = "application"
security_groups = ["sg-0abc1234"]
subnets = ["subnet-0abc1234", "subnet-1abc1234"]
enable_deletion_protection = false
tags = {
Name = "example-alb"
}
}
Notes:
security_groups
is a list of security group IDs.subnets
specifies the subnets for the load balancer.
5. Amazon VPC (Virtual Private Cloud)
Resource Block:
resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "example-vpc"
}
}
Notes:
cidr_block
defines the IP address range for the VPC.
6. Amazon IAM Role
Resource Block:
resource "aws_iam_role" "example" {
name = "example-role"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "ec2.amazonaws.com",
},
},
],
})
tags = {
Name = "example-role"
}
}
Notes:
assume_role_policy
defines the policy that grants an entity permission to assume the role.
7. Amazon DynamoDB
Resource Block:
resource "aws_dynamodb_table" "example" {
name = "example-table"
billing_mode = "PROVISIONED"
hash_key = "id"
read_capacity = 5
write_capacity = 5
attribute {
name = "id"
type = "S"
}
tags = {
Name = "example-table"
}
}
Notes:
hash_key
specifies the primary key attribute.billing_mode
can bePROVISIONED
orPAY_PER_REQUEST
.
8. Amazon SQS (Simple Queue Service)
Resource Block:
resource "aws_sqs_queue" "example" {
name = "example-queue"
tags = {
Name = "example-queue"
}
}
Notes:
name
specifies the name of the queue.
9. Amazon SNS (Simple Notification Service)
Resource Block:
resource "aws_sns_topic" "example" {
name = "example-topic"
tags = {
Name = "example-topic"
}
}
Notes:
name
specifies the name of the SNS topic.
10. Amazon CloudWatch Alarm
Resource Block:
resource "aws_cloudwatch_alarm" "example" {
alarm_name = "example-alarm"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 60
statistic = "Average"
threshold = 80
alarm_actions = [
"arn:aws:sns:us-west-2:123456789012:example-topic"
]
tags = {
Name = "example-alarm"
}
}
Notes:
metric_name
specifies the metric to monitor.threshold
is the value that triggers the alarm.
Summary
These resource blocks provide a foundational setup for some of the most commonly used AWS services. Customize the configurations according to your specific requirements and environment.🚀