Complete the provider configuration to specify the AWS region for the S3 bucket to reduce data transfer costs.
provider "aws" { region = "[1]" } resource "aws_s3_bucket" "example" { bucket = "my-bucket" }
Choosing the correct AWS region like eu-west-1 helps reduce data transfer costs by keeping data close to users or other AWS resources.
Complete the code to enable VPC endpoints to reduce data transfer costs between EC2 and S3.
resource "aws_vpc_endpoint" "s3_endpoint" { vpc_id = aws_vpc.main.id service_name = "com.amazonaws.[1].s3" vpc_endpoint_type = "Gateway" }
The service name must match the region where your VPC is located. Using us-west-2 ensures the VPC endpoint connects correctly and reduces data transfer costs.
Fix the error in the security group rule to allow inbound traffic only from the same VPC to reduce data transfer costs.
resource "aws_security_group_rule" "allow_internal" { type = "ingress" from_port = 0 to_port = 65535 protocol = "tcp" cidr_blocks = ["[1]"] security_group_id = aws_security_group.main.id }
Using the VPC CIDR block 10.0.0.0/16 restricts traffic to within the VPC, reducing data transfer costs by avoiding public internet routing.
Fill in the blank to configure an S3 bucket policy that allows access only from a specific VPC endpoint to reduce data transfer costs.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": ["arn:aws:s3:::my-bucket", "arn:aws:s3:::my-bucket/*"],
"Condition": {
"StringEquals": {
"aws:sourceVpce": "[1]"
}
}
}
]
}Using the correct VPC endpoint ID like vpce-0a1b2c3d4e5f6g7h8 in the bucket policy restricts access to that endpoint, reducing data transfer costs by avoiding public internet traffic.
Fill all three blanks to create a CloudWatch alarm that triggers when data transfer out exceeds a threshold, helping monitor and control costs.
resource "aws_cloudwatch_metric_alarm" "data_transfer_alarm" { alarm_name = "HighDataTransferOut" comparison_operator = "[1]" evaluation_periods = 1 metric_name = "NetworkOut" namespace = "AWS/EC2" period = 300 statistic = "[2]" threshold = [3] alarm_description = "Alarm when data transfer out exceeds threshold" actions_enabled = true }
The alarm triggers when the Average of NetworkOut is GreaterThanThreshold 1,000,000,000 bytes (about 1 GB), helping you monitor data transfer costs.