Motivated by the Docker course from @LINUXtips, I did a bit automation to provision an infrastructure as Code on AWS and then use Ansible to install Docker on an EC2 instance. My plan is provision new resources on the AWS Cloud and write code using Terraform and Ansible with CI/CD tools to help all professionals who are starting their DevOps journey.
- Requirements
- How does it work?
- Setup AWS credentials
- Create a AWS Key pairs
- Create the Terraform files
- Terraform CLI Usage
- Contributing
- AWS Account
- Ansible
- Terraform
- AWS CLI
- Linux CLI
Basically, the Terraform will create some resources on AWS, such as, EC2, SG and the Ansible will be invoked via Terraform resources (local-exec) to call Ansible Roles for then to install the Docker app on EC2 instance.
Use the AWS Documentation to setup your AWS Credentials, basically you have some ways to do this, for example via credentials file or environment variables:
- In my case I used the
~/.aws/credentials
file to create my credentials:
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Now you can create your Key pairs on AWS following the AWS Documentation below:
Remember you can create a Terraform resource to provision automatically:
resource "aws_key_pair" "deployer" {
key_name = "deployer-key"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 email@example.com"
}
It's the time to create the Ansible Roles:
$ ansible-galaxy init docker
According to the best practices from the Terraform documentation, is necessary create several files ".tf" to keep the environment working fine, the Terraform will read all files ".tf" and will provision on the AWS Cloud.
- provider.tf
- securityGroups.tf
- ec2Intance.tf
Main commands:
init Prepare your working directory for other commands
validate Check whether the configuration is valid
plan Show changes required by the current configuration
apply Create or update infrastructure
destroy Destroy previously-created infrastructure
Execute these commands after get completed before steps:
$ terraform init
$ terraform plan
$ terraform apply
$ terraform destroy
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.