--- title: "manually create lambda function" draft: false chapter: false hidden: true --- ## Create Lambda Execution Role 1. Go to IAM Console, choose **Roles** on the left side bar 1. click **Create Role** 1. Under Choose the service that will use this role, choose **Lambda** 1. Click **Next: Permissions**, **Next: Tags**, **Next: Review** 1. In the Review page, enter `alexa-lambda-role` for the **Role name**, and choose **Create role** 1. Click the `alexa-lambda-role`, under **Permissions** tab, click **Add inline policy** 1. In Create Policy page, select **JSON**, and copy & paste the following policy. Please remember to replace ``. Check your DynamoDB table name in DynamoDB Console. If you followed [Build Device Binding UI]({{< ref "1.build-device-ui.md" >}}), a DynamoDB table has already been created. This policy grant the Lambda to read items from device table and put logs to CloudWatch. ```json { "Version": "2012-10-17", "Statement": [ { "Action": [ "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": [ "*" ], "Effect": "Allow" }, { "Action": [ "dynamodb:Query" ], "Resource": [ "arn:aws:dynamodb:*:*:table//index/ByUsernameThingName" ], "Effect": "Allow" }, { "Action": [ "iot:UpdateThingShadow" ], "Resource": [ "*" ], "Effect": "Allow" } ] } ``` 1. Click **Review Policy** 1. In the **Name** field, input a name for this policy 1. Click **Create Policy** ## Create the Lambda function 1. Go to [Lambda Console](https://console.aws.amazon.com/lambda/home?region=us-east-1), click **Create function** 1. Select Author from scratch, and enter the following information: - Name: Provide a name for your Lambda function - Runtime: To use the sample code provided in this topic, choose **Node.js 10.x** - Role: select a role [you previously created](#create-lambda-execution-role) ![](/images/smart-home/create-lambda-1.png) 1. Click **Create Function**. Your function should be created and you will move to **Configuration** 1. In the **Configuration** designer, under **Add triggers** select the **Alexa Smart Home trigger** 1. In the **Configure triggers** section, add the **Skill ID** from the developer console in the box specified. 1. Leave **Enable trigger** checked. This enables the Amazon Alexa service to call your Lambda function. 1. Click **Add** and then click **Save** ## Upload Code 1. Firstly, you should git clone [this repo](https://github.com/aws-samples/aws-alexa-workshop-smarthome), go to root folder in your Cloud9 environment, and type in terminal ```shell git clone https://github.com/aws-samples/aws-alexa-workshop-smarthome cd aws-alexa-workshop-smarthome ``` 1. Edit `config.json` file, you can find these information in AWS Console and Alexa Console 1. Run `npm install --production` to install dependencies 1. Make a zip file to include `index.js`, `auth.js`, `config.json`, `alexa/` and `node_modules/`, these files/directories should be located at the root level of the zip file ``` zip -q -r lambda.zip index.js auth.js config.json alexa/ node_modules/ ``` ![](/images/smart-home/lambda-file-structure.png) 1. Download the zip file to local 1. Go to AWS Lambda Console, click the lambda function 1. Under **Function code**, click **Upload** to upload the zip file 1. Click the **Save** button at the top right corner