Provision infrastructure with Cloud-Init
When you create a generic compute resource in Terraform, your virtual machine (VM) may not have much capability because it is a "fresh" install and needs to be provisioned with the software you want to use. Manually installing the necessary software and its respective dependencies on each VM is time consuming and difficult to maintain at scale.
cloud-init
is a standard configuration support tool available on most Linux distributions and all major cloud providers. cloud-init
allows you to pass a shell script to your instance that installs or configures the machine to your specifications.
In this tutorial, you will create a Terraform instance with the user_data
to deploy a Go web app and SSH key to the newly created device, allowing you to SSH into the machine without a password and start the app with that user.
Prerequisites
For this tutorial, you will need the following:
- Terraform v1.1+ installed locally.
- An AWS account
Clone the example repository here.
Change into your cloned repo directory.
Create a local SSH key
For this tutorial, create a local SSH key to pair with the new terraform
user you create on this instance.
Generate a new SSH key in your terminal called tf-cloud-init
. The argument provided with the -f
flag creates the key in the current directory and creates two files called tf-cloud-init
and tf-cloud-init.pub
. Change the placeholder email address to your email address.
When prompted, press enter to leave the passphrase blank on this key.
Add your public SSH key to your cloud-init script
Open the scripts/add-ssh-web-app.yaml
file and paste the contents of tf-cloud-init.pub
into the user data ssh_authorized_keys
section. You will pass this cloud-init script to your instance resource's user_data
attribute.
For more information on creating a cloud-init
script, refer to the cloud-init documentation.
Review the cloud-init script in the Terraform configuration
Open the main.tf
file located in the instances
directory. Notice how the file
function loads the contents of
the add-ssh-web-app.yaml
file. Then, the configuration passes the contents of
the file into aws_instance.web
as a user_data
value to be initialized when
the instance is created.
Next, change into the directory containing your configuration.
Initialize your configuration.
Apply your configuration. Enter yes
when prompted to create your instance.
When the apply run completes, your terminal will display your instance's IP address.
You have successfully provisioned your AWS instance with cloud-init
. This instance should already be configured with the SSH key, allowing you to connect to it. Your instance should also contain the GoLang demo app.
In the next section, you will SSH into this instance with your local key and start the demo app.
Verify your instance
Connect to your instance via SSH by piping the aws_instance.web.public_ip
resource attribute to the terraform console
command.
Note
Cloud-Init runs after the instance is provisioned, and may take a few minutes to complete. If you receive a "Connection refused" or similar error from the next few commands, wait a few minutes and try again.
Respond to the confirmation prompt with a yes
, and SSH will connect to the
instance.
Now you have SSH access to your AWS instances without creating SSH keys in AWS. This is useful if your organization maintains keypairs outside of AWS.
While still connected to the instance, launch the demo application.
Note
The application will print out your instance's public IP address and continue
running. Press Ctrl-C
to exit the application.
In your web browser, navigate to the IP address of your instance and port 8080 to see the app you deployed.
Destroy your image
Avoid unnecessary charges in your AWS account by destroying your instance in Terraform.
Type yes
when you are prompted in your terminal to delete your infrastructure.
Next Steps
In this tutorial, you deployed a webapp and configured an instance with cloud-init
.
- To learn about creating images with Packer for Terraform deployments, check out the Provision Infrastructure with Packer tutorial.
- For more information about creating templates with
cloud-init
, visit the data provider documentation.