Skip to content

☸️🚀🏗️A bit automation with Terraform to provision an EC2 instance (infrastructure as Code) on AWS and using Ansible to install Docker on this instance. A excellent hands-on to practice the union of Terraform and Ansible working together.

License

Notifications You must be signed in to change notification settings

amaurybsouza/iac-practice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Terraform & Ansible: Deep dive into IaC

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.

Table of Contents

Requirements

  • AWS Account
  • Ansible
  • Terraform
  • AWS CLI
  • Linux CLI

How does it work?

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.

Setup AWS credentials

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

Create a AWS Key pairs

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"
}

Defining the Ansible Roles

It's the time to create the Ansible Roles:

$ ansible-galaxy init docker

Create the Terraform files

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

Terraform CLI Usage

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

Contributing

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.

License

MIT

About

☸️🚀🏗️A bit automation with Terraform to provision an EC2 instance (infrastructure as Code) on AWS and using Ansible to install Docker on this instance. A excellent hands-on to practice the union of Terraform and Ansible working together.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages