Skip to content

This repository provides Terraform codes to create AWS EC2 instances along with a Bastion instance and mount EFS, making it easy to set up EC2 instances in an AWS cloud environment and share files via EFS.

Notifications You must be signed in to change notification settings

s-sasaki-earthsea-wizard/terraform-for-ec2-with-efs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

terraform-for-ec2-with-efs

Overview

This repository contains Terraform codes to create AWS EC2 instances and mount EFS on them.

Development environment

  • OS: MacOS 13.2.1
  • Terraform: v1.4.2
  • AWS CLI: 2.10.0

Installation

Terraform installation

Please install Terraform according to official document;

Environmental variables

Set your AWS access key and secret access key in the bash shell as follows:

export AWS_ACCESS_KEY_ID="your_access_key"
export AWS_SECRET_ACCESS_KEY="your_secret_access_key"

Also, set your key pair name, VPC ID, and subnet ID in the bash shell as follows:

export TF_VAR_key_name="your_key_pair_name"
export TF_VAR_vpc_id="your_vpc_id"
export TF_VAR_subnet_id="your_subnet_id"

These can be also declared in *.tfvars

Usage

Initializaion

Initialize the Terraform project by running:

terraform init

The Makefile enables to do same running by:

make init

Check the Changes to Resources

Review the planned changes by running:

make plan

Here, *.tfvars file is assumed to be default file name terraform.tfvars

Apply the changes

Apply the Terraform code to the resources by running:

make apply

Destruction (if needed)

If you would like to clean up the resources, you can do so by running:

make destroy

Connection to Resources

Add ssh key

Add ssh key running by:

eval "$(ssh-agent -s)"
ssh-add ${your_key_pem_file}

ssh connection to bastion host

You can connect to bastion host running by:

ssh -i ${your_pem_key} ubuntu@<bastion_host_public_ip>

You can see the public IP of bastion host runnning by:

aws ec2 describe-instances --filters "Name=tag:Name,Values=bastion-host" "Name=instance-state-name,Values=running" --query "Reservations[*].Instances[*].PublicIpAddress" --output text

Access right to pem file

If you cannot connect bastion host, please run:

chmod 400 ${your_pem_key}

Of course, tou can also use AWS console.

Others

Trouble shooting

Check cloud resource building log running by:

cat /var/log/cloud-init-output.log
cat /etc/resolv.conf

Usage of graphviz

It is usefull to visualize resources by using graphviz. It can be installed running by:

sudo apt install graphviz

If graphviz is installed successfully, running:

dot -V

should show the Graphviz version.

You can viaualize the graph by:

terraform graph -draw-cycles | dot -Tsvg > graph.svg

About

This repository provides Terraform codes to create AWS EC2 instances along with a Bastion instance and mount EFS, making it easy to set up EC2 instances in an AWS cloud environment and share files via EFS.

Topics

Resources

Stars

Watchers

Forks