Create the Lambda

For this section we want to create the lambda and connect it to the Microsoft SQL Server we created.

In this step, we will use the Amazon.Lambda.Tools, that we installed in the Create Development Environment step to get the code for our Lambda and API Gatway.

  1. From the Cloud9 Development environment, in the bottom right hand corner there should be a terminal window open.
  2. Run the following command to get the lambda code:

    git clone https://github.com/StevenDavid/win403-lambda.git
    
  3. Change the directory to the src code directory:

    cd "win403-lambda/src/win403-lambda/"
    
  4. In the cloud9 file explorer, navigate to the ““win403-lambda/src/win403-lambda/Models/helpers.cs” file and open it.

  5. We need to update lines 24 and 26 of the helpers.cs file with the host address and password for your database. The host endpoint can be found by going to RDS > databases > mssql1 > Connectivity & security tab and coping the endpoint. You need to remember the password you chose when launching the database.

    currentCSObject.host = "{host}";
    currentCSObject.username = "admin";
    currentCSObject.password = "{password}";
    
  6. Save and close the helpers.cs file.

  7. Open the “win403-lambda/src/win403-lambda/aws-lambda-tools-defaults.json” file and update the s3-bucket with the name of the bucket you created earlier.

  8. Save and close the aws-lambda-tools-defaults.json file.

  9. Open the “win403-lambda/src/win403-lambda/serverless.template” file.

  10. We need to update the SecurityGroupIds with the id of the default security group for your account.

    1. Start by logging into the AWS Console.
    2. On the AWS management console home page, search for “Ec2” in the “Find Services” search box. Press enter or click the “Ec2” service that populates in the fast search.
    3. From the Ec2 dashboard page, scroll down the left hand navigation to the “NETWORK & SECURITY” section. Click “Security Groups”
    4. Once on the Security groups page, search for the “default” security group. Copy the “Group ID”
    5. Paste that “group ID” in for the SecurityGroupIds value.
  11. Next we need to updated the SubnetIds. We will use the same subnets that our database is using.

    1. Start by logging into the AWS Console.
    2. On the AWS management console home page, search for “RDS” in the “Find Services” search box. Press enter or click the “RDS” service that populates in the fast search.
    3. From the left navigation select “Databases”.
    4. On the Databases page, select the “mssq1”.
    5. On the “Connectivity & security” tab, scroll down to the subnets. Pick any two to add to the SubnetIds.
  12. Save and close the serverless.template file.

Congratulations! You now have working code that we need to deploy.