This guide will help you deploy a serverless WordPress site on AWS using Terraform.
Prerequisites
- Terraform installed on your machine.
- AWS CLI configured with appropriate permissions.
- Basic understanding of AWS services such as Lambda, API Gateway, S3, and RDS.
Step 1: Setup Terraform Configuration
Create a new directory for your Terraform configuration files.
mkdir terraform-wordpress
cd terraform-wordpress
Create a main.tf
file and add the following basic configuration:
provider "aws" {
region = "us-west-2"
}
resource "aws_s3_bucket" "wordpress_bucket" {
bucket = "your-wordpress-bucket"
acl = "public-read"
}
resource "aws_s3_bucket_object" "wordpress_files" {
bucket = aws_s3_bucket.wordpress_bucket.bucket
key = "index.html"
source = "path/to/your/index.html"
acl = "public-read"
}
Step 2: Setup AWS Lambda
Add the following to main.tf
to create a Lambda function:
resource "aws_lambda_function" "wordpress_lambda" {
filename = "path/to/your/lambda.zip"
function_name = "wordpress_function"
role = aws_iam_role.lambda_exec_role.arn
handler = "index.handler"
runtime = "nodejs14.x"
source_code_hash = filebase64sha256("path/to/your/lambda.zip")
}
Create an IAM role for Lambda:
resource "aws_iam_role" "lambda_exec_role" {
name = "lambda_exec_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "lambda.amazonaws.com"
}
},
]
})
}
resource "aws_iam_role_policy_attachment" "lambda_policy" {
role = aws_iam_role.lambda_exec_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
Step 3: Setup API Gateway
Add the following to main.tf
to create an API Gateway:
resource "aws_api_gateway_rest_api" "wordpress_api" {
name = "wordpress_api"
description = "API for serverless WordPress"
}
resource "aws_api_gateway_resource" "wordpress_resource" {
rest_api_id = aws_api_gateway_rest_api.wordpress_api.id
parent_id = aws_api_gateway_rest_api.wordpress_api.root_resource_id
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "wordpress_method" {
rest_api_id = aws_api_gateway_rest_api.wordpress_api.id
resource_id = aws_api_gateway_resource.wordpress_resource.id
http_method = "ANY"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "wordpress_integration" {
rest_api_id = aws_api_gateway_rest_api.wordpress_api.id
resource_id = aws_api_gateway_resource.wordpress_resource.id
http_method = aws_api_gateway_method.wordpress_method.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.wordpress_lambda.invoke_arn
}
resource "aws_lambda_permission" "api_gateway" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.wordpress_lambda.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.wordpress_api.execution_arn}/*/*"
}
Step 4: Setup RDS for WordPress Database
Add the following to main.tf
to create an RDS instance:
resource "aws_db_instance" "wordpress_db" {
allocated_storage = 20
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
name = "wordpressdb"
username = "admin"
password = "yourpassword"
parameter_group_name = "default.mysql5.7"
skip_final_snapshot = true
}
Step 5: Deploy the Infrastructure
Initialize Terraform and apply the configuration:
terraform init
terraform apply
Step 6: Configure WordPress
After the infrastructure is deployed, configure WordPress to use the RDS database and S3 bucket for media uploads. Update your wp-config.php
file with the database connection details and use a plugin like WP Offload Media to integrate with S3.
Conclusion
By following these steps, you can deploy a serverless WordPress site on AWS using Terraform. This setup leverages AWS Lambda, API Gateway, S3, and RDS to create a scalable and cost-effective WordPress hosting solution.