Create a Lambda Function for MC Data APIs
There are many ways to fire a Trigger API request when uploading a file to an external location such as Amazon S3. This tutorial shows how to use Amazon Web Services (AWS) lambda functionality to authenticate with Marketing Cloud. Then, how to fire a trigger API request after you upload a file to an S3 bucket.
Setup Installed Package
Your new AWS lambda function can access your Marketing Cloud data after you create an installed package in Salesforce Marketing Cloud.
- From Setup, find and select Platform Tools.
- From Platform Tools, find and select Apps.
- From Apps, find and select Installed Packages.
- Create a new installed package.
Description values that you want.
- Add a component under the newly created Installed Package.
- The component is an API integration type.
- The component is a Server-to-Server integration type.
- Give the component the permissions for your lambda task:
Set Up Secrets in AWS
To encrypt your SFMC installed package information at rest, store the information in AWS Secrets Manager. To further restrict values, authenticate using identity and access management (IAM). Store the Marketing Cloud Client ID, Client Secret, Authentication Base URI, and REST Base URI in AWS Secrets Manager.
In the AWS Web Console, navigate to Secrets Manager.
Select Store a new secret.
Select Other type of secret as the Secret type.
Add your Client ID, Client Secret, Authentication Base URI, and REST Base URI from the end of Setup Installed Package [task topic 1]. The sample lambda code uses the following keys mapped to their associated values.
You can select any encryption key for this operation. The names of the keys can be whatever you need.
- To navigate to the Configure secret step, select Next
- Configure the name, description, tags, resource permissions, and replication as necessary for your use case. You're using SFMC_Installed_Package for this example.
- To navigate to the Configure rotation step, select Next.
- Configure rotation on this page as applicable to your own setup.
- Select Next and review the summary. When you're satisfied with it, select Store button to create your secret.
Create an IAM User
To allow your AWS lambda function to access your newly created secret, create an IAM user with access to Secrets Manager.
Navigate to IAM in the AWS Web Console.
To create a user, select Add users.
Enter a username. This example uses SFMC_API_Client.
To enable the IAM user to authenticate in your AWS Lambda function, select Access key - Programmatic access.
To configure the user’s permissions, click Next: Permissions.
Enter a Group name for your AWS Secrets Manager. This example uses SecretsAccess.
To give access to AWS Secrets Manager in that group, add SecretsManagerReadWrite policy.
To move to the user’s tags, select Create group and Next: Tags.
Add any applicable tags to your new IAM users, then select Next: Review.
Select Create user.
Create a Lambda Function
Make an AWS Lambda function that creates a data extension in your SFMC account. This example uses Python 3.7 for setting up the lambda function. You need the Access Key ID and Secret Access Key from your IAM user for subsequent steps in this document.
In the AWS Web Console navigate to Lambda.
Select Create function.
Select the Lambda type Author from scratch.
Enter a function name. In this example, the lambda function is named FILE_TRIGGER_AUTOMATION.
Select Runtime Python 3.7.
Select Create function.
Selecting a different runtime can require importing more dependencies. The sample lambda code shown requires Python 3.7.
Configure the Lambda Function
Tie all the previous steps together, set up environment variables and configure the lambda function to access the SFMC secrets and fire the trigger API request.
- In the AWS Web Console, navigate to the lambda function.
- Copy the Sample Lambda Code to the Code tab, and save your changes.
Sample Lambda Code
In the Configuration tab, select Environment variables.
Select Add environment variable for each key value pair.
- Enter the key AWS_ACCESS and the value <SECRET_ARN>.
- Enter key AWS_SECRET with value <IAM_ACCESS_KEY>.
- Enter key AWS_SECRET_NAME with value <IAM_ACCESS_KEY>.
- Enter key SFMC_FILE_LOCATION_EXTERNAL_KEY with value <SFMC_FILE_LOCATION_EXTERNAL_KEY> This is the external key used in Marketing Cloud’s File Location configuration.
Select the General configuration section.
Select Edit, and then change the Timeout value to at least 30 seconds. That interval is the minimum time required for the lambda function to authenticate to Marketing Cloud and fire the Trigger API request.
Configure the S3 Bucket
- On the S3 bucket where files are uploaded, navigate to the Properties tab, and select Create event notification.
- Enter an Event name and any optional configuration parameters.
- Select the Event types that fire the lambda function.
- Choose the lambda function (File_Trigger_Automation) previously created, and then save your changes.
Test With a File
- To test your new lambda function create a trigger automation in Automation Studio.
- Upload a qualifying file to the S3 bucket location.
- Confirm that your automation started.