Infrastructure as Code examples with Terraform and Ansible for AWS.
- Persisting Terraform State in S3 Back End
- Using variables and providers
- Deploying VPCs, Internet GWs, and Subnets
- Deploying Multi-Region VPC Peering
- Deploying Security groups
- Using Data Source (SSM Parameter Store) to Fetch AMI IDs
- Deploying Key Pairs for App Nodes
- Create EC2 instances
- Destroy Terraform objects
Example of how save the Terraform state file in S3 bucket. Changes on the backend can be found on the backend.tf file
First we need to create a S3 bucket file:
aws s3api create-bucket --bucket <BUCKET_NAME>
Then we need to initialize the backend with the following command:
terraform init
Example of how to use variables and providers in Terraform can be found in: variables.tf and providers.tf
Example of how to deploy in Terraform AWS VPCs, Internet GWs, and Subnets can be found in: networks.tf
Example of how to deploy in Terraform Multi-Region VPC peerings can be found in: networks.tf
Example of how to deploy Security Groups in Terraform can be found in security_groups.tf
Example of how to use Data source to fetch AMI ids can be found in instances.tf
To create the authentication key pairs for SSH use the following command:
ssh-keygen -t rsa
A public and a private key will be created in your ~/.ssh directory
The example of how to atach the key pairs in the EC2 instances can be found in: instances.tf
Example of how to create the EC2 instances can be found in: instances.tf
A example of how to get the ips of the created instances can be found: outputs.tf
Example of how to run Terraform provisioners with Ansible can be found on the instances.tf file.
Files used by Ansible:
- inventory_aws/tf_aws_ec2.yml
- ansible_templates/jenkins-master-sample.yml
- ansible_templates/jenkins-worker-sample.yml
- ansible.cfg
If you are testing and want a destroy all remote objects managed by this Terraform configuration use:
terraform destroy