Get started with ColdFusion AWS Lambda

Overview

AWS Lambda is a compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes. With AWS Lambda module in ColdFusion, you can invoke AWS Lambda functions.

To get started with AWS Lambda, you first create a lambda function in AWS using any supported language. You can then invoke these functions using the Lambda module in ColdFusion.

For more information, see AWS Lambda.

Get started

Install awslambda package

Adobe ColdFusion (2021 release) is modularized, if you are only using the ZIP installer. By default, the module for AWS Lambda is not installed. The first step is to install the Lambda package in ColdFusion.

Note: If you are using the GUI installer, the packages are pre-installed.

The package for Lambda is called awslambda.

To install the package awslambda, use the Package Manager page in the ColdFusion Administrator, or follow the steps below:

Navigate to <CF_HOME>/cfusion/bin.

Enter the command:

  • Windows: cfpm.bat
  • Linux: cfpm.sh

Enter the command, install awslambda.

Wait for the AWS Lambda package to get installed.

For more information, see ColdFusion Package Manager.

Get credentials to access AWS Lambda

When you interact with AWS, you specify your AWS security credentials to verify your credentials and check whether you have permission to access the resources that you are requesting.

AWS uses the security credentials to authenticate and authorize your requests.

You must get the AWS Access Key ID and the AWS Secret Access Key. For more information, see Access Keys.

Add cloud service credentials and configuration

In ColdFusion (2021 release), there is a method getCloudService() that gives you a handle to create objects for accessing various cloud services.

The syntax of the service handle is as follows:

service=getCloudService(cloudCred,cloudConfig)

Where:

  • cloudCred: Defines the credentials for the cloud service. It could either be a struct or a string (also known as credential alias).
  • cloudConfig: Defines the cloud service configuration details. It could either be a struct or a string (also known as config alias).

After you've acquired the AWS credentials, you must declare these credentials in one of the following ways. Only then you can use these credentials to create a Lambda object, after which you can use the object to make calls to the Lambda methods.

ColdFusion Administrator

SET CREDENTIALS

In the ColdFusion Administrator, click Data & Services > Cloud Credentials.

An alias is a named representation of a cloud service and its configuration details. You can set the config alias through ColdFusion Administrator.

After entering the details, click Add Credential.

SET CONFIGURATION OPTIONS

In the ColdFusion Administrator, click Data & Services > Cloud Configuration.

Enter the following details, like configuration Alias, Vendor, and the name of the service.

After adding the configuration options, you may need to add a few more options. You can do so in the next screen. The following are a few options that you may need to add:

  • Request config
  • Client config
  • Proxy settings
  • Retry policy
  • Retry conditions

For more information, see Cloud configuration options.

CREATE THE OBJECT

Once you've created the aliases for Lambda credential and configuration options, you can create the object by using the cloudService API, and include the following in your CFM.

lambdaObject= getCloudService(lambdaCred, lambdaConf)

Application.cfc

You can specify the Lambda credentials and configuration options in Application.cfc. For example,

component 
{  
    void function onApplicationStart(){ 
        application.awsCred  = { 
                                "alias" : "simple_lambda", 
                                "vendorName" : "AWS", 
                                "region" : "us-east-1", 
                                "secretAccessKey" : "xxxxxxxxxxxxxxxxx", 
                                "accessKeyId" : "xxxxxxxxxxxxxxxx" 
                             } 
              
        application.lambdaConf  = { 
                                "serviceName" : "LAMBDA" 
                             }      
    } 
}

CREATE THE OBJECT

lambdaObject = getCloudService(application.awsCred, application.lambdaConf)

On CFM page

On a CFM page, you can specify the Lambda credentials and configuration options in one of the four methods, specified below:

CREDENTIAL ALIAS AND CONFIGURATION ALIAS

After you've created the aliases for Lambda credential and configuration options, you can use these aliases in the getCloudService handle as shown below:

<cfscript> 
  // define the credential and the configuration aliases in the ColdFusion Admin 
  lambda=cloudService(awsCred,lambdaConf) 
  // code below. 
  ........... 
</cfscript>

CREDENTIAL ALIAS AND STRUCT FOR CONFIGURATION OPTIONS

<cfscript> 
    // Using credential alias and struct for service config 
    lambdaConf = { 
            "alias":"simple_lambda", 
            "serviceName" : "LAMBDA", 
            "clientOverrideConfig":{ 
                "retryPolicy":{ 
                  "numRetries":4 
                } 
            }, 
            "httpClientConfig":{ 
                "maxConnections":50 
            } 
   
    } 
    lambdaObject= cloudService("awsCred", lambdaConf) 
   
    // code below 
    ..................... 
</cfscript>

CONFIGURATION ALIAS AND STRUCT FOR CREDENTIALS

<cfscript> 
    // Using config alias and struct for service credentials 
    lambdaCred={ 
      "vendorName":"AWS", 
      "alias": "lambda_cred_alias", 
      "region":"us-east-2", 
      "accessKeyId": "access key", 
      "secretAccessKey": "secret access" 
    } 
    lambdaObject = cloudService(lambdaCred, "lambdaConf") 
    // code below 
    ..................................... 
</cfscript>

STRUCTS FOR BOTH CREDENTIAL AND CONFIGURATION OPTIONS

<cfscript> 
  // Using Structs for both cloud credential and config 
  lambdaCred={ 
    "vendorName":"AWS", 
    "alias": "lambda_cred_alias", 
    "region":"us-east-2", 
    "accessKeyId": "access key", 
    "secretAccessKey": "secret access key" 
  } 
   
  lambdaConf = { 
          "alias":"lambda_conf_alias", 
          "serviceName" : "LAMBDA", 
          "clientOverrideConfig":{ 
              "retryPolicy":{ 
                "numRetries":4 
              } 
          }, 
          "httpClientConfig":{ 
              "maxConnections":50 
          } 
   
  } 
  lambdaObject = cloudService(lambdaCred, lambdaConf ) 
   
  // code below 
  ................................................................... 
</cfscript>

Admin API

You can also add Lambda credentials and configuration options by using the Admin APIs. The methods to add credentials and configuration are available in cloud.cfc.

The examples below demonstrate the usage of the methods addCredential(struct credential) and addServiceConfig(struct config).

ADD CREDENTIALS

<cfscript> 
  // Create an object of administrator component and call the login method 
  adminObj = createObject("component","cfide.adminapi.administrator") 
  adminObj.login("admin") 
   
  // Create an object of cloud component 
  cloudObj = createObject("component","cfide.adminapi.cloud") 
   
  // define credentials struct 
  credentialStruct={ 
    "alias" : "LambdaCredential", 
    "vendorName" : "AWS", 
    "region" : "us-east-2", 
    "secretAccessKey" : "secret access key", 
    "accessKeyId" : "access key" 
  } 
   
  // add credential credentialStruct 
  try{ 
    cloudObj.addCredential(credentialStruct) 
    writeOutput("Credentials added successfully") 
  } 
  catch(any e){ 
      writeDump(e) 
  } 
</cfscript>

ADD CONFIGURATION

<cfscript> 
  // Create an object of administrator component and call the login method 
  adminObj = createObject("component","cfide.adminapi.administrator") 
  adminObj.login("admin") 
   
  // Create an object of cloud component 
  cloudObj = createObject("component","cfide.adminapi.cloud") 
   
  // define configuration struct 
  configStruct={ 
      "alias":"LambdaConf", 
      "serviceName":"LAMBDA", 
      "clientOverrideConfig":{ 
          "retryPolicy":{ 
              "numRetries":4 
          } 
      }, 
      "httpClientConfig":{ 
          "maxConnections":50 
      } 
  } 
   
  // add config configStruct 
  try{ 
    cloudObj.addServiceConfig(configStruct) 
    writeOutput("Configuration service added successfully") 
  } 
  catch(any e){ 
    writeDump(e) 
  } 
</cfscript>

ColdFusion Lambda APIs

ListFunctions

Returns a list of Lambda functions.

Parameters

Parameter

Description

masterRegion

The AWS region of the master functions. If specified, you must set FunctionVersion to ALL.

functionVersion

Set to ALL to include entries for all published versions of each function.

marker

Defines the token for pagination to return the next page of results.

maxItems

The maximum number of functions to return in the response. 

For more information, see ListFunctions.

Example

<cfscript>
    lambdaSrv = getCloudService(credentialsStruct, confStruct)
    // params = StructNew();
    functionsList = lambdaSrv.ListFunctions()
    writeDump(functionsList)
</cfscript>

InvokeFunction

Invokes a Lambda function. You can invoke a function synchronously or asynchronously.

Request parameters

Parameter

Description

payload

Input JSON to the Lambda function.

invocationType

Choose one of the following:

  • RequestResponse (default): Invoke the function synchronously. Keep the connection open until the function returns a response or times out. The API response includes the function response and additional data.
  • Event: Invoke the function asynchronously. Send events that fail multiple times to the function's dead-letter queue (if it's configured). The API response only includes a status code.
  • DryRun: Validate parameter values and verify that the user or role has permission to invoke the function.

logType

Include the execution log in the response. Choose either None or Tail.

clientContext

Base64-encoded data to pass in the function. The size limit is 3583 bytes.

Example

<cfscript>
    lambdaSrv = getCloudService(credentialsStruct, confStruct)
    lambdaFunctionARN = "lambda-function-name|ARN|partialARN"
    requestOptions = {
        "InvocationType" = "RequestResponse|Event|DryRun"
        "LogType" = "Tail|None",
        "PayLoad" = "{Event-JSON-str}"
    }
    responseStatus = lambdaSrv.InvokeFunction(lambdaFunctionARN, requestOptions)
</cfscript>

 Adobe

Recevez de l’aide plus rapidement et plus facilement

Nouvel utilisateur ?