WordPress’s built-in cron system, WP-Cron, runs scheduled tasks when users visit a page. This can impact performance and reliability, especially for low-traffic sites. While a traditional cron job is an option, it’s less ideal for containerized WordPress deployments.
A more robust solution, which I also used for my blog, involves using an AWS Lambda function written in GO triggered by an EventBridge Scheduler. This Lambda function initiates WP-Cron via an HTTP GET request to the WordPress instance’s private IP.
A unique URL is used consisting of the private IP for the wordpress instance and a random string appended to /wp-cron.php?lambda_doing_cron_
, which bypasses any caching. The Host
header is set to the WordPress domain to ensure proper routing, as the request uses the private IP address for the WordPress Instance. The function handles errors, logs activity, and requires appropriate IAM permissions for outbound HTTP requests.
The Go code that I wrote for this Lambda function is shown below.
package main
// Copyright 2025 Karun Dambiec
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"time"
"github.com/aws/aws-lambda-go/lambda"
)
func generateRandomString() string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
rand.Seed(time.Now().UnixNano()) // Seed random number generator with current time
result := make([]byte, 10) // Create a 10 character random string
for i := range result {
result[i] = charset[rand.Intn(len(charset))]
}
return string(result)
}
func HandleRequest() (string, error) {
// URL to send the GET request to
randomString := generateRandomString()
url := "http://127.0.0.1/wp-cron.php?lambda_go_doing_cron_" + randomString
fmt.Print("GET Request to URL: ")
fmt.Println(url)
// Create a new HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatalf("Failed to create request: %v", err)
return "", err
}
// Set custom Host header
req.Host = "www.myblog.com" // Adjust to the correct Host header value
// Create a new HTTP client
client := &http.Client{}
// Perform the GET request using the client
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Failed to make GET request: %v", err)
return "", err
}
defer resp.Body.Close()
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Failed to read response body: %v", err)
return "", err
}
// Return the response body as a string (for example purposes)
return fmt.Sprintf("Response from the website: %s", body), nil
}
func main() {
// Start the Lambda function
lambda.Start(HandleRequest)
}
A Lambda function written the Go programming Language can be packaged as a docker image which can then be pushed to ECR (Elastic Container Registry) on AWS. Refer to the following AWS documentation: https://docs.aws.amazon.com/lambda/latest/dg/go-image.html#go-image-provided
For the Dockerfile for this Lambda function I used the following code:
FROM golang:1.24.0 as build
WORKDIR /wordpresslambda
# Copy dependencies list
COPY go.mod go.sum ./
# Build with optional lambda.norpc tag
COPY main.go .
RUN go build -tags lambda.norpc -o main main.go
# Copy artifacts to a clean image
FROM public.ecr.aws/lambda/provided:al2023
COPY --from=build /wordpresslambda/main ./main
ENTRYPOINT [ "./main" ]
The docker image for the lambda function can then be built using the following command.
docker buildx build --platform linux/amd64 --provenance=false -t wordpress-cron:latest .
After the docker image has been built you can then push it to Elastic Container Registry in your AWS account.
A copy of the code from this post can be found in the following Github repository: https://github.com/kdambiec/public-go-wordpress-cron-lambda
Leave a reply