- Create the IAM user
- Sign in to the AWS Management Console and open the IAM console (search for “IAM” at the top).
- In IAM, choose Users, then click Create user.
- Enter a username (for example:
terraform). - For Access type, enable Programmatic access (this generates the Access Key ID and Secret Access Key Terraform will use).

- Attach a policy
- For these exercises, attach the
AmazonVPCFullAccessmanaged policy to limit permissions to the VPC-related resources used in the examples. - If you prefer to experiment with fewer restrictions, you may attach
AdministratorAccesstemporarily — but follow least-privilege principles in real projects and production.

- Verify permissions
- Click the newly created user and open the Permissions tab.
- Confirm that the
AmazonVPCFullAccesspolicy (or whichever policy you attached) is listed.

- Create access keys
- Open the Security credentials tab for the user.
- Scroll to the Access keys section and choose Create access key.
- Confirm any prompts and generate the keys.
- Copy the Access Key ID and the Secret Access Key — you’ll need them to configure Terraform locally.

Important: copy the Secret Access Key now — it is shown only once. If you lose it you must recreate the access key pair.
- Environment variables (
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN,AWS_DEFAULT_REGION) - AWS shared credentials/config files (
~/.aws/credentialsand~/.aws/config) - Instance profile / IAM role (EC2, ECS tasks, Lambda)
- Container credentials or other platform-specific providers
aws configure). For automation, use IAM roles or short-term credentials (STS).
Example Terraform configuration
Save the following HCL in a .tf file in your working directory (for example main.tf). This sample creates two VPCs and one subnet:
- Use the appropriate AWS region for your resources (
us-east-1,us-west-2, etc.). - These exports persist only for the active shell session. To make them persistent, add them to your shell profile (e.g.,
~/.bashrc,~/.zshrc).
- Access Key ID
- Secret Access Key
- Default region
- Default output format
~/.aws/credentials and ~/.aws/config which Terraform will automatically read.
Credential configuration options (quick reference)
Security best practices
Do not leave access keys lying around. Delete keys after finishing exercises or rotate them regularly. For long-lived automation, prefer IAM roles, instance profiles, or short-term STS credentials.
For repeated use, prefer the AWS shared credentials file or IAM roles. Use least-privilege policies (for example
AmazonVPCFullAccess for VPC-only exercises) instead of AdministratorAccess.- In the directory containing your
.tffiles, initialize Terraform and create a plan:
.tf files.
Troubleshooting tip
- If Terraform fails to authenticate, verify:
- Environment variables are set in the same shell session.
~/.aws/credentialshas the correct keys if using the AWS CLI.- The user has the required IAM policy attached.
- AWS IAM documentation: https://docs.aws.amazon.com/iam/
- AWS CLI
configuredocumentation: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html - Terraform AWS provider: https://registry.terraform.io/providers/hashicorp/aws/latest