Complete the following steps to create a Lambda function.
1. Open the Lambda console at https://console.aws.amazon.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 function.
3. On the Create function page, keep Author from scratch selected.
4. Under Basic information, do the following:
• For Name, type examplecorp_lambda_tin_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_tin_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;
const ses = new AWS.SES();
const crypto = require('crypto');
exports.handler = (event, context, callback) => {
var eventdata = JSON.parse(event.body);
var length = 16; //Adjust this value to shorten or lengthen the AS2 username - 32 chars is max length an AS2 username can be
var username = crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length); //generates a random string
/*
Username could also be the name or email parameter from the event body
var username = eventdata.name
var username = eventdata.email
*/
console.log("username: " + username);
var params = {
FleetName: '<Fleet-Name>', /* required */
StackName: '<Stack-Name>', /* required */
UserId: username,
Validity: 5 //TTL of URL
};
createas2streamingurl(params, eventdata, 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, sesdata, awsRequestId, callback) {
var request = appstream.createStreamingURL(params);
request.
on('success', function (response) { //Successful Response
console.log("Success! AS2 Streaming URL created.");
var output = response.data;
var url = output.StreamingURL;
sendEmail(sesdata); //With a successful AS2 URL generated, trigger the SES SendEmail function
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) { //an error occoured
console.log("error: " + JSON.stringify(response.message));
errorResponse('Error creating AS2 streaming URL.', awsRequestId, callback);
}).
send();
}
function sendEmail(data) {
var sender = "<sender@example.com>"; //Sender needs to be a verified address in SES
var receiver = data.email.trim(); /*Trim the string of any preceding and trailing whitespaces*/
var name = data.name.trim();
var params = {
Destination: {
ToAddresses: [receiver]
},
Message: {
Body: {
Text: {
Data: 'Hello ' + name + ',' + '\n\nThank you for trying Example Corp\'s Application Suite.',
Charset: 'UTF-8'
}
},
Subject: {
Data: 'Thank you for trying Example Corp ' + name,
Charset: 'UTF-8'
}
},
Source: sender
};
console.log("Sending Email.");
ses.sendEmail(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
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.