""" Terraform Resource Example Snippets ==================================== Curated minimal working examples for top AWS resources. Sourced from Terraform Registry docs — NOT from IaC-Eval dataset. These help cheap LLMs understand correct usage patterns. """ # Minimal HCL snippets showing correct usage patterns # Key: resource_type -> example code snippet RESOURCE_EXAMPLES = { "aws_instance": '''resource "aws_instance" "example" { ami = data.aws_ami.ubuntu.id instance_type = "t3.micro" subnet_id = aws_subnet.example.id vpc_security_group_ids = [aws_security_group.example.id] tags = { Name = "example" } }''', "aws_vpc": '''resource "aws_vpc" "example" { cidr_block = "10.0.0.0/16" enable_dns_support = true enable_dns_hostnames = true tags = { Name = "example" } }''', "aws_subnet": '''resource "aws_subnet" "example" { vpc_id = aws_vpc.example.id cidr_block = "10.0.1.0/24" availability_zone = "us-east-1a" tags = { Name = "example" } }''', "aws_security_group": '''resource "aws_security_group" "example" { name = "example" description = "Example security group" vpc_id = aws_vpc.example.id ingress { from_port = 443 to_port = 443 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } }''', "aws_s3_bucket": '''resource "aws_s3_bucket" "example" { bucket = "my-example-bucket" tags = { Name = "example" } }''', "aws_s3_bucket_versioning": '''resource "aws_s3_bucket_versioning" "example" { bucket = aws_s3_bucket.example.id versioning_configuration { status = "Enabled" } }''', "aws_s3_bucket_acl": '''resource "aws_s3_bucket_acl" "example" { bucket = aws_s3_bucket.example.id acl = "private" depends_on = [aws_s3_bucket_ownership_controls.example] }''', "aws_s3_bucket_ownership_controls": '''resource "aws_s3_bucket_ownership_controls" "example" { bucket = aws_s3_bucket.example.id rule { object_ownership = "BucketOwnerPreferred" } }''', "aws_iam_role": '''resource "aws_iam_role" "example" { name = "example-role" assume_role_policy = data.aws_iam_policy_document.assume.json }''', "aws_iam_policy_document": '''data "aws_iam_policy_document" "assume" { statement { actions = ["sts:AssumeRole"] principals { type = "Service" identifiers = ["ec2.amazonaws.com"] } } }''', "aws_iam_role_policy_attachment": '''resource "aws_iam_role_policy_attachment" "example" { role = aws_iam_role.example.name policy_arn = "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess" }''', "aws_iam_instance_profile": '''resource "aws_iam_instance_profile" "example" { name = "example-profile" role = aws_iam_role.example.name }''', "aws_iam_policy": '''resource "aws_iam_policy" "example" { name = "example-policy" policy = data.aws_iam_policy_document.example.json }''', "aws_db_instance": '''resource "aws_db_instance" "example" { identifier = "example-db" engine = "postgres" engine_version = "15" instance_class = "db.t3.micro" allocated_storage = 20 storage_type = "gp3" storage_encrypted = true username = "admin" password = var.db_password db_subnet_group_name = aws_db_subnet_group.example.name vpc_security_group_ids = [aws_security_group.db.id] multi_az = false publicly_accessible = false skip_final_snapshot = true backup_retention_period = 7 }''', "aws_db_subnet_group": '''resource "aws_db_subnet_group" "example" { name = "example" subnet_ids = [aws_subnet.a.id, aws_subnet.b.id] tags = { Name = "example" } }''', "aws_lambda_function": '''resource "aws_lambda_function" "example" { function_name = "example-function" role = aws_iam_role.example.arn handler = "index.handler" runtime = "nodejs18.x" filename = data.archive_file.example.output_path source_code_hash = data.archive_file.example.output_base64sha256 }''', "archive_file": '''data "archive_file" "example" { type = "zip" source_file = "${path.module}/lambda/index.js" output_path = "${path.module}/lambda.zip" }''', "aws_lambda_permission": '''resource "aws_lambda_permission" "example" { statement_id = "AllowAPIGatewayInvoke" action = "lambda:InvokeFunction" function_name = aws_lambda_function.example.function_name principal = "apigateway.amazonaws.com" }''', "aws_route53_zone": '''resource "aws_route53_zone" "example" { name = "example.com" comment = "Example hosted zone" tags = { Name = "example" } } resource "aws_route53_zone" "private" { name = "private.example.com" vpc { vpc_id = aws_vpc.example.id } }''', "aws_route53_record": '''resource "aws_route53_record" "example" { zone_id = aws_route53_zone.example.zone_id name = "www.example.com" type = "A" ttl = 300 records = [aws_instance.example.public_ip] } resource "aws_route53_record" "weighted" { zone_id = aws_route53_zone.example.zone_id name = "api.example.com" type = "A" ttl = 60 set_identifier = "primary" weighted_routing_policy { weight = 70 } records = ["1.2.3.4"] }''', "aws_route53_query_log": '''resource "aws_route53_query_log" "example" { cloudwatch_log_group_arn = aws_cloudwatch_log_group.example.arn zone_id = aws_route53_zone.example.zone_id }''', "aws_route53_zone_association": '''resource "aws_route53_zone_association" "example" { zone_id = aws_route53_zone.example.zone_id vpc_id = aws_vpc.example.id }''', "aws_route53_vpc_association_authorization": '''resource "aws_route53_vpc_association_authorization" "example" { zone_id = aws_route53_zone.example.zone_id vpc_id = aws_vpc.example.id }''', "aws_lb": '''resource "aws_lb" "example" { name = "example-lb" internal = false load_balancer_type = "application" security_groups = [aws_security_group.example.id] subnets = [aws_subnet.a.id, aws_subnet.b.id] }''', "aws_lb_target_group": '''resource "aws_lb_target_group" "example" { name = "example-tg" port = 80 protocol = "HTTP" vpc_id = aws_vpc.example.id }''', "aws_lb_listener": '''resource "aws_lb_listener" "example" { load_balancer_arn = aws_lb.example.arn port = 80 protocol = "HTTP" default_action { type = "forward" target_group_arn = aws_lb_target_group.example.arn } }''', "aws_lb_target_group_attachment": '''resource "aws_lb_target_group_attachment" "example" { target_group_arn = aws_lb_target_group.example.arn target_id = aws_instance.example.id port = 80 }''', "aws_elb": '''resource "aws_elb" "example" { name = "example-elb" availability_zones = ["us-east-1a"] listener { instance_port = 80 instance_protocol = "http" lb_port = 80 lb_protocol = "http" } health_check { target = "HTTP:80/" interval = 30 timeout = 5 healthy_threshold = 2 unhealthy_threshold = 2 } }''', "aws_eks_cluster": '''resource "aws_eks_cluster" "example" { name = "example-cluster" role_arn = aws_iam_role.example.arn vpc_config { subnet_ids = [aws_subnet.a.id, aws_subnet.b.id] } }''', "aws_elastic_beanstalk_application": '''resource "aws_elastic_beanstalk_application" "example" { name = "example-app" description = "Example Elastic Beanstalk Application" }''', "aws_elastic_beanstalk_environment": '''resource "aws_elastic_beanstalk_environment" "example" { name = "example-env" application = aws_elastic_beanstalk_application.example.name solution_stack_name = "64bit Amazon Linux 2 v5.8.0 running Node.js 18" }''', "aws_cloudwatch_log_group": '''resource "aws_cloudwatch_log_group" "example" { name = "/aws/example" retention_in_days = 14 }''', "aws_cloudwatch_log_resource_policy": '''resource "aws_cloudwatch_log_resource_policy" "example" { policy_document = data.aws_iam_policy_document.example.json policy_name = "example-policy" }''', "aws_cloudfront_distribution": '''resource "aws_cloudfront_distribution" "example" { origin { domain_name = aws_s3_bucket.example.bucket_regional_domain_name origin_id = "S3Origin" origin_access_control_id = aws_cloudfront_origin_access_control.example.id } enabled = true default_root_object = "index.html" default_cache_behavior { allowed_methods = ["GET", "HEAD"] cached_methods = ["GET", "HEAD"] target_origin_id = "S3Origin" viewer_protocol_policy = "redirect-to-https" forwarded_values { query_string = false cookies { forward = "none" } } } restrictions { geo_restriction { restriction_type = "none" } } viewer_certificate { cloudfront_default_certificate = true } }''', "aws_cloudfront_origin_access_control": '''resource "aws_cloudfront_origin_access_control" "example" { name = "example-oac" origin_access_control_origin_type = "s3" signing_behavior = "always" signing_protocol = "sigv4" }''', "aws_dynamodb_table": '''resource "aws_dynamodb_table" "example" { name = "example-table" billing_mode = "PAY_PER_REQUEST" hash_key = "id" attribute { name = "id" type = "S" } }''', "aws_kinesis_stream": '''resource "aws_kinesis_stream" "example" { name = "example-stream" shard_count = 1 retention_period = 24 }''', "aws_kinesis_stream_consumer": '''resource "aws_kinesis_stream_consumer" "example" { name = "example-consumer" stream_arn = aws_kinesis_stream.example.arn }''', "aws_kinesis_firehose_delivery_stream": '''resource "aws_kinesis_firehose_delivery_stream" "s3_example" { name = "example-stream" destination = "extended_s3" extended_s3_configuration { role_arn = aws_iam_role.example.arn bucket_arn = aws_s3_bucket.example.arn } } resource "aws_kinesis_firehose_delivery_stream" "es_example" { name = "es-stream" destination = "elasticsearch" s3_configuration { role_arn = aws_iam_role.example.arn bucket_arn = aws_s3_bucket.example.arn } elasticsearch_configuration { domain_arn = aws_elasticsearch_domain.example.arn index_name = "example" role_arn = aws_iam_role.example.arn } }''', "aws_codebuild_project": '''resource "aws_codebuild_project" "example" { name = "example-project" service_role = aws_iam_role.example.arn artifacts { type = "NO_ARTIFACTS" } environment { compute_type = "BUILD_GENERAL1_SMALL" image = "aws/codebuild/standard:5.0" type = "LINUX_CONTAINER" } source { type = "GITHUB" location = "https://github.com/example/repo.git" } }''', "aws_internet_gateway": '''resource "aws_internet_gateway" "example" { vpc_id = aws_vpc.example.id tags = { Name = "example" } }''', "aws_route_table": '''resource "aws_route_table" "example" { vpc_id = aws_vpc.example.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.example.id } tags = { Name = "example" } }''', "aws_route_table_association": '''resource "aws_route_table_association" "example" { subnet_id = aws_subnet.example.id route_table_id = aws_route_table.example.id }''', "aws_elasticache_cluster": '''resource "aws_elasticache_cluster" "example" { cluster_id = "example-cluster" engine = "redis" node_type = "cache.t3.micro" num_cache_nodes = 1 port = 6379 }''', "aws_ami": '''data "aws_ami" "example" { most_recent = true owners = ["amazon"] filter { name = "name" values = ["amzn2-ami-hvm-*-x86_64-gp2"] } }''', "aws_availability_zones": '''data "aws_availability_zones" "available" { state = "available" filter { name = "opt-in-status" values = ["opt-in-not-required"] } }''', "aws_lex_intent": '''resource "aws_lex_intent" "example" { name = "ExampleIntent" fulfillment_activity { type = "ReturnIntent" } conclusion_statement { message { content = "Your order has been placed." content_type = "PlainText" } } }''', "aws_redshift_cluster": '''resource "aws_redshift_cluster" "example" { cluster_identifier = "example-cluster" database_name = "exampledb" master_username = "admin" master_password = "Password123!" node_type = "dc2.large" cluster_type = "single-node" skip_final_snapshot = true }''', "aws_sns_topic": '''resource "aws_sns_topic" "example" { name = "example-topic" display_name = "Example Topic" tags = { Name = "example" } }''', "aws_sqs_queue": '''resource "aws_sqs_queue" "example" { name = "example-queue" delay_seconds = 0 max_message_size = 262144 message_retention_seconds = 86400 visibility_timeout_seconds = 30 tags = { Name = "example" } }''', "aws_cloudwatch_event_rule": '''resource "aws_cloudwatch_event_rule" "example" { name = "example-rule" schedule_expression = "rate(1 hour)" }''', "aws_cloudwatch_event_target": '''resource "aws_cloudwatch_event_target" "example" { rule = aws_cloudwatch_event_rule.example.name target_id = "ExampleTarget" arn = aws_lambda_function.example.arn }''', "aws_api_gateway_rest_api": '''resource "aws_api_gateway_rest_api" "example" { name = "example-api" description = "Example REST API" }''', "aws_api_gateway_resource": '''resource "aws_api_gateway_resource" "example" { rest_api_id = aws_api_gateway_rest_api.example.id parent_id = aws_api_gateway_rest_api.example.root_resource_id path_part = "example" }''', "aws_api_gateway_method": '''resource "aws_api_gateway_method" "example" { rest_api_id = aws_api_gateway_rest_api.example.id resource_id = aws_api_gateway_resource.example.id http_method = "GET" authorization = "NONE" }''', "aws_api_gateway_integration": '''resource "aws_api_gateway_integration" "example" { rest_api_id = aws_api_gateway_rest_api.example.id resource_id = aws_api_gateway_resource.example.id http_method = aws_api_gateway_method.example.http_method integration_http_method = "POST" type = "AWS_PROXY" uri = aws_lambda_function.example.invoke_arn }''', "aws_efs_file_system": '''resource "aws_efs_file_system" "example" { creation_token = "example-efs" tags = { Name = "example" } }''', "aws_efs_mount_target": '''resource "aws_efs_mount_target" "example" { file_system_id = aws_efs_file_system.example.id subnet_id = aws_subnet.example.id security_groups = [aws_security_group.example.id] }''', "aws_msk_cluster": '''resource "aws_msk_cluster" "example" { cluster_name = "example" kafka_version = "3.5.1" number_of_broker_nodes = 3 broker_node_group_info { instance_type = "kafka.m5.large" client_subnets = [aws_subnet.a.id, aws_subnet.b.id, aws_subnet.c.id] storage_info { ebs_storage_info { volume_size = 100 } } security_groups = [aws_security_group.example.id] } }''', "aws_msk_serverless_cluster": '''resource "aws_msk_serverless_cluster" "example" { cluster_name = "example" vpc_config { subnet_ids = [aws_subnet.a.id, aws_subnet.b.id] security_group_ids = [aws_security_group.example.id] } client_authentication { sasl { iam { enabled = true } } } }''', "aws_backup_vault": '''resource "aws_backup_vault" "example" { name = "example-backup-vault" kms_key_arn = aws_kms_key.example.arn tags = { Name = "example" } }''', "aws_backup_plan": '''resource "aws_backup_plan" "example" { name = "example-plan" rule { rule_name = "example-rule" target_vault_name = aws_backup_vault.example.name schedule = "cron(0 12 * * ? *)" } }''', "aws_backup_selection": '''resource "aws_backup_selection" "example" { iam_role_arn = aws_iam_role.example.arn name = "example-selection" plan_id = aws_backup_plan.example.id resources = [aws_instance.example.arn] }''', "aws_kinesis_analytics_application": '''resource "aws_kinesis_analytics_application" "example" { name = "example-application" inputs { name_prefix = "example" schema { record_columns { name = "example" sql_type = "VARCHAR(64)" } record_format { mapping_parameters { json { record_row_path = "$" } } } } kinesis_stream { resource_arn = aws_kinesis_stream.example.arn role_arn = aws_iam_role.example.arn } } }''', "aws_kinesisanalyticsv2_application": '''resource "aws_kinesisanalyticsv2_application" "example" { name = "example-application" runtime_environment = "FLINK-1_18" service_execution_role = aws_iam_role.example.arn }''', "aws_chime_voice_connector": '''resource "aws_chime_voice_connector" "example" { name = "example-vc" require_encryption = true }''', "aws_chime_voice_connector_streaming": '''resource "aws_chime_voice_connector_streaming" "example" { voice_connector_id = aws_chime_voice_connector.example.id disabled = false data_retention = 7 streaming_notification_targets = ["SQS"] }''', "aws_redshift_subnet_group": '''resource "aws_redshift_subnet_group" "example" { name = "example" subnet_ids = [aws_subnet.a.id, aws_subnet.b.id] }''', "aws_redshift_endpoint_access": '''resource "aws_redshift_endpoint_access" "example" { endpoint_name = "example" subnet_group_name = aws_redshift_subnet_group.example.name cluster_identifier = aws_redshift_cluster.example.cluster_identifier }''', "aws_redshift_event_subscription": '''resource "aws_redshift_event_subscription" "example" { name = "example" sns_topic_arn = aws_sns_topic.example.arn source_type = "cluster" source_ids = [aws_redshift_cluster.example.id] event_categories = ["configuration", "management"] }''', "aws_redshift_parameter_group": '''resource "aws_redshift_parameter_group" "example" { name = "example" family = "redshift-1.0" }''', "aws_s3_bucket_request_payment_configuration": '''resource "aws_s3_bucket_request_payment_configuration" "example" { bucket = aws_s3_bucket.example.id payer = "Requester" }''', } def get_examples_for_resources(resource_types: list[str]) -> str: """Return formatted example snippets for given resource types.""" # Merge hand-curated and auto-generated examples try: from tf_examples_generated import GENERATED_EXAMPLES except ImportError: GENERATED_EXAMPLES = {} snippets = [] for rt in resource_types: if rt in RESOURCE_EXAMPLES: snippets.append(f"# Example: {rt}\n{RESOURCE_EXAMPLES[rt]}") elif rt in GENERATED_EXAMPLES: snippets.append(f"# Example: {rt}\n{GENERATED_EXAMPLES[rt]}") if not snippets: return "" return "Reference examples (adapt names and values to the request):\n\n" + "\n\n".join(snippets) if __name__ == "__main__": test = ["aws_instance", "aws_vpc", "aws_subnet", "aws_security_group"] print(get_examples_for_resources(test)) print(f"\nTotal examples: {len(RESOURCE_EXAMPLES)}")