Complete the following steps to create a Lambda function.
1. Open the Lambda console at https://console.thinkwithwp.com/lambda/.
2. Do one of the following:
• If you haven’t created any Lambda functions, a Getting Started page displays. Under Getting Started, choose Create a function.
• If you have created a Lambda function, in the upper right corner of the Functions page, choose Create a function.
3. On the Create function page, keep Author from scratch selected.
4. Under Basic information, do the following:
• For Name, type examplecorp_lambda_saas_function.
• For Runtime, choose Node.js 8.10.
5. Under Permissions, choose the icon next to Choose or create an execution role. Then do the following:
• For Execution role, choose Use an existing role.
• For Existing role, choose examplecorp_lambda_saas_role from the list.
6. Choose Create function.
7. In the Function code section, on the index.js tab, the placeholder code displays. Delete the placeholder code, and copy and paste the following code onto the tab:
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
const AWS = require('aws-sdk');
const appstream = new AWS.AppStream;
exports.handler = (event, context, callback) => {
if (!event.requestContext.authorizer) { //checks to see if Cognito Authorization has been configured
errorResponse('Authorization has not been configured, please configure an authorizer in API Gateway', context.awsRequestId, callback);
return;
}
const username = event.requestContext.authorizer.claims['cognito:username'];
var params = {
FleetName: '<Fleet-Name>', /* required */
StackName: '<Stack-Name>', /* required */
UserId: username,
Validity: 5
};
createas2streamingurl(params, context.awsRequestId, callback);
};
function errorResponse(errorMessage, awsRequestId, callback) { //Function for handling error messaging back to client
callback(null, {
statusCode: 500,
body: JSON.stringify({
Error: errorMessage,
Reference: awsRequestId,
}),
headers: {
'Access-Control-Allow-Origin': '<origin-domain>', //This should be the domain of the website that originated the request, example: amazonaws.com
},
});
}
function createas2streamingurl(params, awsRequestId, callback) {
var request = appstream.createStreamingURL(params);
request.
on('success', function (response) {
console.log("Success. AS2 Streaming URL created.");
var url = response.data.StreamingURL;
callback(null, {
statusCode: 201,
body: JSON.stringify({
Message: url,
Reference: awsRequestId,
}),
headers: {
'Access-Control-Allow-Origin': '<origin-domain>', //This should be the domain of the website that originated the request, example: amazonaws.com
},
});
}).
on('error', function (response) {
console.log("Error: " + JSON.stringify(response.message));
errorResponse('Error creating AS2 streaming URL.', awsRequestId, callback);
}).
send();
}
8. Replace the following variables with your own values:
• <Stack-Name>
• <Fleet-Name>
• <origin-domain>
Where:
• <Stack-Name> is the name of the stack from which you want to create the streaming URL.
• <Fleet-Name> is the name of the fleet that is associated with the stack from which you want this function to generate streaming URLs.
• <origin-domain> is the domain of the website originating the request to API Gateway, in this case the full S3 website URL (ex. http://bucket-name.s3-website.region.amazonaws.com), that you set up for this project.
9. Save the function and close the Lambda console.