Crash Code-10 (Terraform EC2 SSH Keys & Cloud-Config Guide)
Learn how to generate secure RSA SSH keys and render an AWS EC2 cloud-config YAML file using Terraform's tls and local providers in this concise guide
760 Words [Mind Tax: 3:27m]
06 September 2026, 6:30:00 PM
Problem Description
Your company has started using AWS, and needs an EC2 instance containing generated SSH keys. Your task is to prepare its configuration.
Requirements
- Add the following providers to your solution using Terraform 0.14.5 syntax:
- local(version
2.0.0) - tls(version
3.0.0)
- Reference the above providers in your solution via the
providerkeyword and add their configuration if required. - Don’t add any more providers.
- Use the correct resource from the
tlsprovider to generate private and public SSH keys (RSA -4096bit) using one of the aforementioned providers. The name of this resource should bessh_key. - Use the correct resource from the
localprovider to generatecloud-config.yamlby renderingcloud-config.yaml.tpland filling placeholders with the following keys:
public_keywith the result of creating the public keyprivate_keywith the result of creating the private key
- The name of this resource should be
cloud_config. Remember to use correct indentation.
Assumptions
- You don’t need to write any code to set up any resources in AWS.
- cloud-config.yaml.tpl follows precisely the following format (don’t copy it, assume it is located in the same directory as cloud-config.yaml.tpl, simply read it):
# cloud-config.yaml.tpl
write_files:
- path: /etc/app/key
permissions: '0600'
content: |
${private_key}
- path: /etc/app/key.pub
permissions: '0644'
content: |
${public_key}
- Indents in the YAML file are four single spaces.
- The version of Terraform in use is 0.14.5.
Hints
- Do not use deprecated Terraform syntax; e.g. use
var.whateverinstead of${var.whatever}. - Use template functions in place of deprecated
templateproviders.
Approach
To fulfill these requirements, we must construct a Terraform solution that generates cryptographic materials natively and writes them to a local file formatted for AWS cloud-init.
Step 1: Provider Block Configuration
Terraform version 0.14.5 introduced a stricter, cleaner dependency management model. We will use the terraform { required_providers { ... } } block to strictly pin the local and tls providers to versions 2.0.0 and 3.0.0 respectively. Following this, we explicitly instantiate them using the provider keyword.
Step 2: Generating the Cryptographic Keys
We utilize the tls_private_key resource from the TLS provider. Setting the algorithm to RSA and the bit length to 4096 ensures a highly secure SSH key pair. The attributes private_key_pem and public_key_openssh will be dynamically exposed upon creation.
Step 3: Template Rendering
Older Terraform versions relied on the template_file data source, but modern industry standards dictate using the built-in templatefile() function. This function reads a file from disk and parses it, swapping out placeholder variables (like ${private_key}) with dynamic values injected via a map. Finally, the local_file resource outputs the rendered content to cloud-config.yaml.
Solve
| |
Why This Solution Is Correct
- Syntax Compliance: By declaring variables in the templatefile function map directly as private_key = tls_private_key.ssh_key.private_key_pem instead of wrapping them in string interpolation ("${…}"), we follow the modern HCL2 best practices outlined in the hints.
- Built-in Functions: The deprecated template provider is entirely avoided. templatefile() securely and efficiently evaluates the file at the supplied path.
- No Unnecessary AWS Resources: As specified in the assumptions, we rely entirely on the tls and local providers to generate the needed configuration without actually calling the AWS provider.
Additional Information
While this solution effectively generates a local configuration file, standard security principles apply when dealing with cryptographic assets in Terraform:
- State File Security: The generated
private_key_pemwill be stored in plain text inside yourterraform.tfstatefile. In a production environment, ensure your state file backend (like an S3 bucket) is encrypted at rest and tightly controls access via IAM policies. - Ephemeral Usage: Generating SSH keys via the tls provider is great for bootstrapping ephemeral environments, CI/CD pipelines, or bastion hosts. For long-lived enterprise environments, it is generally recommended to import existing keys from a secure vault like AWS Secrets Manager or HashiCorp Vault.