This project utilizes Terraform to deploy an EC2 instance on AWS, followed by Ansible to configure the instance by installing Docker using the geerlingguy.docker
Ansible role. A Makefile is included to simplify common Terraform commands.
Terraform must be installed on your machine. You can download and install it from the official site: Terraform Downloads.
The AWS CLI simplifies the configuration of your AWS credentials. Download and install it from the official site: AWS CLI Downloads.
You need to configure your AWS credentials so that Terraform can interact with AWS. Run the following command to set up your credentials:
aws configure
You’ll be prompted for:
- AWS Access Key ID
- AWS Secret Access Key
- Default region name (e.g., us-east-1)
- Default output format (json recommended)
Ansible is required to configure the EC2 instance after it's created. You can download and install it from the official site: Ansible Downloads.
This project uses the Ansible role geerlingguy.docker
to install Docker on the EC2 instance. You need to install this role before running Ansible.
Install the role with the following command:
ansible-galaxy install geerlingguy.docker
Clone this repository to get started with the project:
git clone https://github.com/clementpnn/aws-web-setup.git
cd aws-web-setup
.
├── ansible
│ └── playbook.yml
├── terraform
│ ├── 00_providers.tf
│ ├── 00_variables.tf
│ ├── 01_keypairs.tf
│ ├── 02_networks.tf
│ ├── 03_security_groups.tf
│ ├── 04_instances.tf
│ └── 05_ansible.tf
├── Makefile
├── .gitignore
└── README.md
terraform/
: Contains Terraform configuration files to create the EC2 instance.
ansible/playbook.yml
: Ansible playbook to install Docker on the EC2 instance.
Makefile
: Simplifies the execution of common Terraform commands.
The Makefile includes several commands to facilitate working with Terraform. Below is a summary of the available commands:
Command | Description |
---|---|
make all |
Initializes Terraform and applies the configuration (creates infrastructure). |
make init |
Initializes the Terraform directory (downloads plugins, configures the backend). |
make apply |
Applies the Terraform plan and deploys the infrastructure. |
make destroy |
Destroys all resources managed by Terraform. |
make clean |
Removes locally generated Terraform files, such as state files (.tfstate ). |
Before you can apply or destroy infrastructure, you need to initialize Terraform to download necessary modules and plugins.
Run this command:
make init
To create the infrastructure defined in your Terraform files (e.g., an EC2 instance), run:
make apply
This command automatically applies the Terraform plan and creates all specified resources without prompting for confirmation, thanks to the -auto-approve option
.
To destroy all resources created by Terraform, run:
make destroy
This will remove all AWS resources associated with this configuration, such as the EC2 instance.
If you want to clean up the state files generated by Terraform locally, run the clean
command:
make clean
This command removes the terraform.tfstate
, terraform.tfstate.backup
, and .terraform directory
where temporary files are stored.
To connect to the EC2 server from your terminal, follow these steps:
- Ensure you have the private SSH key that matches the public key used when creating the EC2 instance.
- Identify the public IP address of your EC2 instance. You can obtain this from the Terraform output or the AWS console.
- Use the following SSH command to connect to the server:
ssh -i "~/.ssh/id_rsa" admin@<public_ip>
-i "~/.ssh/id_rsa"
: This specifies the path to the private key (id_rsa
). Adjust the path if your private key is stored elsewhere.admin@<public_ip>
: This specifies the user (admin
for Debian-based images, adjust if you're using a different distribution) and the public IP address of your EC2 instance.
Once connected, you can run any commands on the server, including verifying Docker installation.
If you get an error message like Permission denied (publickey).
, you need to change the permissions of the private key file:
chmod 400 ~/.ssh/id_rsa