---
keyword: [Extend, Third Party Technology, AWS]
nav: wcp_docs
parent_url: /wcp_docs/
title: >- 
  Example: Create an AWS Lambda Function Using SAM CLI
layout: subsection
---

This sample workflow illustrates how to deploy an AWS Lambda function to an AWS organization account. Before you begin, make sure that the following prerequisites are in place:

-   A Workday-provided AWS organization account with short-term credentials obtained from the Workday Developer Site.
-   AWS CLI installed and configured on your local machine.
-   AWS SAM CLI installed on your local machine.
-   Basic knowledge of AWS Lambda and serverless architecture.
-   Determine the AWS region that corresponds to your current company's region. See [Reference: Supported AWS Regions](/wcp_docs/GUID-4c546bf6-25df-4372-aa2a-6e07bb7b1967-enHYPHENus.html).

    <b>Note:</b> If you configure SAM CLI with the incorrect region, you’ll get an error with the `EC2 Error Code: UnauthorizedOperation`.



### Scenario

### Steps

1.  Set Up Your Development Environment to ensure that you have AWS CLI and AWS SAM CLI installed and properly configured on your local machine.

    For additional information, please refer to [AWS SAM Prerequisites](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/prerequisites.html).

2.  Open your terminal or command prompt and navigate to the directory where you want to create your project.
3.  Execute the `sam init --runtime <runtime> --name <project-name>` command to initialize a new SAM project.

    Replace `<runtime>` with the desired programming language runtime for your Lambda function (Example: `python3.8` or `nodejs14.x`) and `<project-name>` with the desired name for your project.

    <b>Note:</b> The project name must start with the `app-` prefix, and can't contain an underscore.

4.  Navigate into the newly created project directory. You’ll find a `template.yaml` file containing the AWS SAM template for your project. Open the file and locate the <b>Resources</b> section. This is where you define your Lambda function.
5.  Replace the existing <b>Resources</b> section with the following code snippet:

    ```
    Resources:
      MyFunction:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: myfunction/
          Handler: app.lambda_handler
          Runtime: <runtime>
          Role: '{{resolve:ssm:/workday-extend-tenant-config/LambdaExecutionRole}}'
          VpcConfig:
            SecurityGroupIds:
              - '{{resolve:ssm:/workday-extend-tenant-config/LambdaSecurityGroup}}'
            SubnetIds:
              - '{{resolve:ssm:/workday-extend-tenant-config/VPCSubnet1}}'
              - '{{resolve:ssm:/workday-extend-tenant-config/VPCSubnet2}}'
    ```

    Replace `<runtime>` with the same runtime you selected in Step 3.

6.  Modify the `samconfig.toml` file in the newly created project directory as follows:

    -   Confirm that the `stack_name` value has the `app-` prefix set.
    -   Under `[default.global.parameters]` add `region = "us-west-2"`.
    -   Under `[default.global.parameters]` add `s3_bucket = "workday-wcp-<org-short-id>-us-west-2"`.
    -   Under `[default.deploy.parameters]` set `resolve_s3 = false`.
    -   Under `[default.package.parameters]` set `resolve_s3 = false`.

    The `samconfig.toml` file should be similar to:

    ```
    # More information about the configuration file can be found here: 
    # [https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html) version = 0.1 
    
    [default] 
    [default.global.parameters]
    stack_name = "app-testapp1"
    region = "us-west-2"
    s3_bucket = "workday-wcp-abcdef-us-west-2"
    
    [default.build.parameters]
    cached = true
    parallel = true
    
    [default.validate.parameters]
    lint = true
    
    [default.deploy.parameters]
    capabilities = "CAPABILITY_IAM"
    confirm_changeset = true
    resolve_s3 = false
    
    [default.package.parameters]
    resolve_s3 = false
    
    [default.sync.parameters]
    watch = true
    
    [default.local_start_api.parameters]
    warm_containers = "EAGER"
    
    [default.local_start_lambda.parameters]
    warm_containers = "EAGER"
    ```

7.  Create a new directory called `myfunction` in the project root.
8.  Inside this directory, create a file named `app.<runtime>` (Example: `app.py` for Python) and write the code for your Lambda function.

    You can use the following code in Python:

    ```
    import json
    
    def lambda_handler(event, context):
        # Your function logic goes here
        response = {
            'statusCode': 200,
            'body': json.dumps('Hello, Workday Extend Account!')
        }
        
        return response
    ```

    You can use the following code in Node.js:

    ```
    exports.handler = async (event) => {
        // Your function logic goes here
        const response = {
            statusCode: 200,
            body: JSON.stringify('Hello, SAM!'),
        };
        return response;
    };
    ```

9.  In your terminal, navigate to the project root directory and execute the `sam build && sam deploy` command to build and deploy your Lambda function.

    Follow the prompts in the deployment wizard to specify the deployment options, such as AWS Region, Stack name, and IAM role.

10. Once the deployment is complete, execute the `aws lambda invoke --function-name <function-name> output.txt` command to test your Lambda function using the AWS CLI.
11. Use the AWS SAM CLI `sam local start-lambda` command to invoke your AWS Lambda function through the AWS CLI or SDKs. This command starts a local endpoint that emulates AWS Lambda.

    ![](GUID-f7dc8c82-091a-4a4a-a8aa-9b0a5d6c8479.png)

    ![](GUID-98a41107-4598-42ee-b6e4-775d3e26ee3e.png)

    ![](GUID-10ac3820-ea4a-4fde-8ab4-bdf7342f99a9.png)



### Related Information

[AWS Lambda](/wcp_docs/GUID-a8a83c29-7a27-434d-88a7-7c24668467e2-enHYPHENus.html)

[Tutorial: Deploying a Hello World application](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html)

