Certified Jenkins Engineer

AWS Lambda and Advanced Deployment Techniques

Demo Lambda Invoke Function

In this lesson, we’ll extend our Jenkins pipeline to invoke an AWS Lambda function automatically after deployment. Instead of copying the function URL from the console by hand, we’ll fetch it dynamically using the AWS CLI and validate the /live endpoint. This streamlines your CI/CD process and ensures your function is responding as expected.

Inspecting the Lambda Function in the Console

Before automating, you can view your function’s public URL and settings in the AWS Lambda console:

The image shows an AWS Lambda console with details of a function named "solar-system-function," including its public URL and configuration settings. The console displays options for adding triggers and destinations, and the function's last modification time.

Retrieving the Function URL via AWS CLI

AWS provides the get-function-url-config command to programmatically retrieve a function’s URL configuration. For full details, see the AWS Lambda CLI command reference.

The image shows a webpage from the AWS CLI Command Reference, listing available commands related to AWS Lambda.

The image shows a webpage from the AWS CLI Command Reference, specifically detailing options for the `get-function-url-config` command related to AWS Lambda functions.

Parsing the JSON Response

When you run:

aws lambda get-function-url-config --function-name solar-system-function

you receive a JSON object containing:

JSON KeyDescription
FunctionArnThe ARN of the Lambda function
FunctionUrlThe public URL endpoint
AuthTypeAuthorization model (e.g., NONE)
CorsCORS settings
CreationTimeTimestamp when the URL config was created
LastModifiedTimeLast update timestamp
InvokeModeInvocation mode (BUFFERED or RESPONSE_STREAM)

Jenkins Pipeline Stage: Lambda Invocation

Below is a Groovy stage that runs on the main branch. It:

  1. Uses AWS credentials stored in Jenkins.
  2. Waits 30 seconds for the Lambda update to propagate.
  3. Fetches and normalizes the FunctionUrl.
  4. Sends a HEAD request to the /live endpoint and checks for a 200 OK response.
stage('Lambda - Invoke Function') {
    when {
        branch 'main'
    }
    steps {
        withAWS(credentials: 'aws-s3-ec2-lambda-creds', region: 'us-east-2') {
            sh '''
            # Wait for Lambda to stabilize
            sleep 30s

            # Fetch the function URL configuration
            function_url_data=$(aws lambda get-function-url-config --function-name solar-system-function)

            # Extract and normalize the FunctionUrl
            function_url=$(echo $function_url_data | jq -r '.FunctionUrl | sub("/$"; "")')

            # Send a HEAD request to the /live endpoint and check for 200 OK
            curl -Is $function_url/live | grep -i "200 OK"
            '''
        }
    }
}

Note

Make sure your IAM user or role for aws-s3-ec2-lambda-creds includes lambda:GetFunctionUrlConfig permissions.

Sample JSON Output

{
  "FunctionArn": "arn:aws:lambda:us-east-2:604513224291:function:solar-system-function",
  "FunctionUrl": "https://gxl2znysqzkhp116766t29be5d9e0jcjm.lambda-url.us-east-2.on.aws/",
  "AuthType": "NONE",
  "Cors": {
    "AllowOrigins": ["*"]
  },
  "CreationTime": "2024-10-01T12:27:05.4573487",
  "LastModifiedTime": "2024-10-01T12:27:05.4573487",
  "InvokeMode": "BUFFERED"
}

Verifying with curl

You can manually test the live endpoint:

curl -Is https://gxl2znysqzkhp116766t29be5d9e0jcjm.lambda-url.us-east-2.on.aws/live

Expected response:

HTTP/1.1 200 OK
Date: Wed, 02 Oct 2024 08:37:51 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 0
Connection: keep-alive
x-amzn-RequestId: b136587b-839f-4904-ad01-032e778b5709
access-control-allow-origin: *
etag: W/"11-oM4ub/mwEIvBYTxJnDIjZC9y6"
x-powered-by: Express
X-Amzn-Trace-Id: Root=1-66fd065f-19a457cb200ddba664c780a;Parent=04f1e27712579323;Sampled=0;Lineage=1:ea66bb95:0

Pipeline Execution Results

In the Jenkins UI, you’ll see the Lambda invocation stage after any tests:

The image shows a Jenkins pipeline for a project named "solar-system" with various stages, some completed successfully and one with a failure in the "Publish Dependency-Check results" step.

Warning

The OWASP Dependency-Check stage is configured with stopBuild: false, so a failure there won’t halt your deployment. Ensure you review those warnings separately.

With this setup, every push to main will automatically confirm that your Lambda function is live and returning the expected HTTP status, fully automating post-deployment validation.

Watch Video

Watch video content

Previous
Demo Update Lambda Configuration