Terraform Project: Sentiment Analysis
Now that you understand the basics of Terraform, let's create a more complex project for our sentiment analysis Lambda function used in the previous class.
Initialize Terraform Project
This creates a new directory for our Terraform project with the following structure we'll build:
Warning
You don't have this structure yet. We will create it in the following steps.
sentiment-analysis-iac-terraform/
├── main.tf # Main Terraform configuration
├── variables.tf # Input variables
├── outputs.tf # Output values
├── terraform.tfvars # Variable values
├── versions.tf # Provider requirements
├── lambda/ # Lambda function code
│ ├── app.py # Lambda function
│ ├── requirements.txt # Python dependencies
│ └── Dockerfile # Docker configuration
└── modules/ # Reusable modules (optional)
Creating the Lambda Function Code
Let's create our sentiment analysis function that will be deployed using Docker.
Create Lambda Directory
Question 1
Lambda Function Code
Question 2
Requirements File for Lambda
Question 3
Dockerfile for Lambda
Question 4
Defining Infrastructure with Terraform
Now let's define our infrastructure using Terraform configuration files.
Get Lambda Execution Role
Question 5
Finding Role ARN 1
If you need to find existing roles, you can list them with:
Create Provider Configuration
Question 6
Create Variables Configuration
Question 7
Create Variable Values File
Question 8
Create Main Terraform Configuration
Question 9
Create Outputs Configuration
Question 10
Secure Your Configuration
Question 11
Deploying with Terraform
Initialize Terraform
Validate Configuration
Format Configuration
Plan the Deployment
Warning
If you see any errors related to Docker, ensure that Docker is running, with proper authorizations:
Also, ensure that you are authenticated with ECR.
ECR Authentication
Replace YOUR_ECR_URL
with your actual ECR URL.
This shows you what Terraform will create, modify, or destroy.
Apply the Configuration
Important!
The deployment process will:
- Create an ECR repository
- Build the Docker image locally
- Push it to Amazon ECR
- Create the Lambda function
- Set up the API Gateway
- Configure all necessary permissions
Info!
Type yes
when prompted to confirm the deployment.
Troubleshooting Deployment Issues
Common Error: Docker Build Failures
If Docker image building fails:
Step 1: Check Docker is running
Step 2: Authenticate with ECR manually
If Deployment Fails
Question 12
Testing the Deployment
Question 13
Question 14
Question 15
Question 16
Question 17
Question 18
Question 19
That's all for today! Let's just clean up the workspace.
Question 20