Skip to content

EC2 — Terraform Example

Progress checklist

This example provisions:

  • An S3 bucket (versioning + SSE-S3 enabled)
  • An S3 Files file system and mount targets in each provided subnet
  • IAM roles (file system role + EC2 instance profile)
  • Security groups (compute → mount target on NFS port 2049)
  • An Amazon Linux 2023 EC2 instance with amazon-efs-utils installed and the file system mounted at /mnt/s3files

Access to the instance is through SSM Session Manager — no SSH key pair or bastion host is needed.

  1. Confirm prerequisites.

    RequirementMinimum version
    Terraform1.5
    AWS provider6.40
    AWS CLI2.34.26 (for aws ssm start-session)

    Install the Session Manager plugin if not already present:

    Terminal window
    aws ssm start-session --version

    If the command is not found, follow the AWS Session Manager plugin installation guide.

  2. Clone the example.

    Terminal window
    git clone https://github.com/jajera/terraform-aws-s3-files.git
    cd terraform-aws-s3-files/examples/ec2

    The example directory contains:

    • Directoryexamples/ec2/
      • main.tf
      • terraform.tfvars.example
      • terraform.tfvars (you create this)
  3. Create terraform.tfvars.

    Copy the example file and set your VPC and subnet IDs:

    Terminal window
    cp terraform.tfvars.example terraform.tfvars

    Edit terraform.tfvars:

    vpc_id = "vpc-0123456789abcdef0"
    subnet_ids = ["subnet-aaaaaaaaaaaaaaaaa", "subnet-bbbbbbbbbbbbbbbbb"]
    # Optional overrides (defaults shown):
    # aws_region = "ap-southeast-6"
    # instance_type = "t3.micro"
    # bucket_name = null # auto-generated: s3files-demo-<random>
    # bucket_force_destroy = true
    VariableDefaultRequiredDescription
    vpc_idyesVPC for mount targets and EC2 instance
    subnet_idsyesSubnets for mount targets (one per AZ recommended)
    instance_subnet_idfirst subnet_ids entrynoSubnet for the EC2 instance
    aws_regionap-southeast-6noAWS region
    instance_typet3.micronoEC2 instance type
    bucket_nameauto-generatednoLeave null to use s3files-demo-<random>
    bucket_force_destroytruenoAllow destroy even when bucket contains objects
  4. Initialise and apply.

    Terminal window
    terraform init
    terraform plan
    terraform apply

    When apply finishes, note the outputs:

    Terminal window
    terraform output

    Key outputs:

    OutputDescription
    instance_idEC2 instance ID (i-…)
    file_system_idS3 Files file system ID
    bucket_nameBacking S3 bucket name
    ami_idAmazon Linux 2023 AMI used
  5. Connect via SSM Session Manager.

    Terminal window
    aws ssm start-session \
    --target "$(terraform output -raw instance_id)" \
    --region ap-southeast-6
  6. Verify the mount.

    Inside the SSM session:

    Terminal window
    df -h /mnt/s3files
    ls -la /mnt/s3files

    Write a file and confirm it appears in S3:

    Terminal window
    echo "hello from terraform ec2" > /mnt/s3files/test.txt
    cat /mnt/s3files/test.txt

    Exit the session when done:

    Terminal window
    exit
  7. Tear down.

    Terminal window
    terraform destroy