以下の手順を実行して Lambda 関数を作成します。
1.Lambda コンソールを https://console.aws.amazon.com/lambda/ で開きます。
2.以下のいずれかを行います。
• 初めて Lambda 関数を作成する場合、[Getting Started] ページが表示されます。[Getting Started] の [Create a function] をクリックします。
• Lambda 関数を作成したことがある場合、[Functions] ページの右上隅にある [Create a function] をクリックします。
3.[Create a function] ページでは、[Author from scratch] が選択されていることを確認します。
4.[Basic information] で以下を実行します。
• [Name] には「examplecorp_lambda_si_function」と入力します。
• [Runtime] で [Node.js 8.10] を選択します。
5.[Permissions] で、[Choose or create an execution role] の横のアイコンをクリックします。次に、以下を実行します。
• [Execution role] で [Use an existing role] を選択します。
• [Existing role] で、リストから [examplecorp_lambda_si_role] を選択します。
6.[Create function] を選択します。
7.[Function code] セクションの [index.js] タブに、プレースホルダーコードが表示されます。プレースホルダーコードを削除し、次のコードをコピーしてタブに貼り付けます。
// 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();
exports.handler = (event, context, callback) => { //Event should contain the Name, Email and AWS Account ID
var eventdata = JSON.parse(event.body); //Parses the JSON from the event body
var params = {
ImagePermissions: { /* required */
allowFleet: true, //Boolean
allowImageBuilder: true //Boolean
},
Name: '<IMAGE_NAME>', /* required */
SharedAccountId: eventdata.account //AWS Account ID parsed from the JSON body
};
shareas2image(params, eventdata, context.awsRequestId, callback); //Share image function call
};
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 shareas2image(params, eventdata, awsRequestId, callback) {
var request = appstream.updateImagePermissions(params);
request.
on('success', function (response) {
console.log("Success. " + JSON.stringify(response.data));
sendEmail(eventdata);
callback(null, {
statusCode: 201,
body: JSON.stringify({
Message: "Success",
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 sharing AS2 Image.', 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 trialing 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. We have shared a preconfigured AppStream 2.0 image with all of Example Corp\'s applications to your AWS Account.',
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
});
}
重要
この関数により、自動的に AppStream 2.0 イメージがエンドユーザーの指定した AWS アカウント ID と共有されます。この関数は、アカウントにイメージのアクセス許可を付与する前に AWS アカウント所有権を検証しません。ソリューションを本稼働にデプロイする際には、必要に応じて独自に検証を行うようにすることを推奨します。たとえば、エンドユーザーのアイデンティティまたは AWS アカウント所有権の検証です。
8.以下の変数を独自の値に置き換えます。
• <Image-Name>
• <origin-domain>
• sender@example.com
各パラメータの意味は次のとおりです。
• <Image-Name> は、共有する AppStream 2.0 イメージの名前です。
• <origin-domain> は、API ゲートウェイにリクエストを送るウェブサイトのドメインです。ここでは、このプロジェクト用に設定した S3 ウェブサイトのフル URL (http://bucket-name.s3-website.region.amazonaws.com) になります。
• 送信者の E メールアドレスは、Amazon SES に追加した検証済みの E メールアドレスです。
9.関数を保存して Lambda コンソールを閉じます。