This repository is an example of packer + ansible + terraform + goss.
It's using Goss tool to do some unit tests on services running, like iptables and ssh
Ansible instals the iptables and configure it to being accessible on port 22.
Terraform uses the custom AMI created by searching for it using aws_ami
resource. It's creating also the key pair to access the instance.
When terraform finishes to create the resources it prints out the NLB domain that can be used to access the instance.
The final architecture is an NLB in front with an instance in private subnet running in an autoscalig group.
- ansible
bastion.yaml
- ansible code to configure the AMI, install iptables and configure it and reconfigure the ssh with some basic security statements.goss.yaml
- goss checks to be performed in the instance on build time.
- scripts
bootstrap.sh
- botstrap script to instanle ansible. It should have a minimal set of commands as the responsible fo the job should be the ansible.
- terraform
- I wont describe all files as I think they are self explanatory and I tried to have a minimal separation by terraform resources(maybe too much separation for this small case).
packer-build.json
- packer definition to build the AMI
- If you have
aws_access_key
andaws_secret_key
please change them inpacker-build.json
file accordingly. If you're using some sort of autoconfigured environment with roles then don't need to specify them. - Change
region
(aws region) accordingly inpacker-build.json
vpc_id
andsubnet_id
are temporary resources to build the AMI. Change them with your own ID's. I recommend to use public subnet for this step.- Execute
packer build -machine-readable packer-build.json
cd ./terraform
vpc_id
,public_subnet_id
andprivate_subnet_id
from vars.tf change to your existing VPC and the subnet ID should be private as this is a bastion server with NLB in frontterraform init
terraform plan
terraform apply
- The terraform will create a local key file named
example.pem
to access the bastion. - In the end of terraform apply it will output the DNS name of NLB that was assigned to the instance and to access it execute this:
ssh <DNS_NAME> -l ec2-user -i example.pem