# Terraform configuration to deploy a simple S3 bucket using a CloudFormation template.

provider "aws" {
  region = "us-east-1"
}

# --- CloudFormation Template for S3 Bucket ---
resource "aws_cloudformation_stack" "s3_bucket_stack" {
  name = "MyTerraformS3BucketStack"

  template_body = <<EOF
AWSTemplateFormatVersion: '2010-09-09'
Description: A simple S3 bucket CloudFormation template for Terraform demo.

Resources:
  MyS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-terraform-cf-bucket-${aws_account_id.current.id}-${data.aws_region.current.name}
      Tags:
        - Key: Environment
          Value: Development
        - Key: Project
          Value: CloudFormationTerraformDemo
    DeletionPolicy: Delete # Ensures bucket is deleted with stack

Outputs:
  BucketName:
    Description: Name of the S3 bucket
    Value: !Ref MyS3Bucket
EOF

  tags = {
    Project = "CloudFormationTerraformDemo"
  }
}

# Data source to get current AWS account ID
data "aws_account_id" "current" {}

# Data source to get current AWS region
data "aws_region" "current" {}

# --- Outputs ---
output "s3_bucket_name" {
  value       = aws_cloudformation_stack.s3_bucket_stack.outputs["BucketName"]
  description = "The name of the S3 bucket created by the CloudFormation stack."
}

output "cloudformation_stack_id" {
  value       = aws_cloudformation_stack.s3_bucket_stack.id
  description = "The ID of the CloudFormation stack."
}
