27 April 2022

Amazon Web Services Kickstart Part 2. Rise of the Lambda

We are looking to get started with Amazon Web Services (AWS). Being complete newbies finding a place to start can be daunting. Never fear you can get started in a few easy steps. We will be setting up an Amazon Web Services (AWS) and installing some command line tools. In some instance corporate intranets or Viratual Private Networks (VPN) may interfere with some functionality of the command line tools. Be aware that there are also plugins for Eclipse and Intellij. At the time of this writing the Eclipse plugins appear somewhat neglected. The last update was several months ago and some irritating bugs are more than two years old. This blog assumes you are running Windows 10.

So I run Powershell and change to the directory where I want to store my source code. I then run the “sam init” command.

sam init results

PS C:\Users\username\source> sam init

You can preselect a particular runtime or package type when using the `sam init` experience.
Call `sam init --help` to learn more.

Which template source would you like to use?
        1 - AWS Quick Start Templates
        2 - Custom Template Location
Choice: 1

Choose an AWS Quick Start application template
        1 - Hello World Example
        2 - Multi-step workflow
        3 - Serverless API
        4 - Scheduled task
        5 - Standalone function
        6 - Data processing
        7 - Infrastructure event management
        8 - Machine Learning
Template: 1

 Use the most popular runtime and package type? (Python and zip) [y/N]: n

Which runtime would you like to use?
        1 - dotnet6
        2 - dotnet5.0
        3 - dotnetcore3.1
        4 - go1.x
        5 - java11
        6 - java8.al2
        7 - java8
        8 - nodejs14.x
        9 - nodejs12.x
        10 - python3.9
        11 - python3.8
        12 - python3.7
        13 - python3.6
        14 - ruby2.7
Runtime: 7

What package type would you like to use?
        1 - Zip
        2 - Image
Package type: 1

Which dependency manager would you like to use?
        1 - gradle
        2 - maven
Dependency manager: 2

Project name [sam-app]: HelloWorldProject

Cloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)

    Generating application:
    Name: HelloWorldProject
    Runtime: java8
    Architectures: x86_64
    Dependency Manager: maven
    Application Template: hello-world
    Output Directory: .

    Next steps can be found in the README file at ./HelloWorldProject/README.md

    Commands you can use next
    [*] Create pipeline: cd HelloWorldProject && sam pipeline init --bootstrap
    [*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch

PS C:\Users\username\source>

This Lambda code that is now generated will Display a Hello World message wrapped in JSON. Additionally it will reach out to an Amazon Web Service called Checkip and retrieve the IP address of where the code is running. Now we will change to the HelloWorldProject\HelloWorldFunction directory and run “mvn clean package”. You should see something similar to the results below. It is possible that a firewall or corporate VPN will block your access to https://checkip.amazonaws.com. If access to the URL is blocked the unit test will fail.

mvn clean package

[INFO] Scanning for projects...
[INFO] -----------------------< helloworld:HelloWorld >------------------------
[INFO] Building A sample Hello World created for SAM CLI. 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ HelloWorld ---

### lots of stuff omitted here  ###

[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing C:\Users\clynch\source\HelloWorldProject\HelloWorldFunction\target\HelloWorld-1.0.jar with C:\Users\clynch\source\HelloWorldProject\HelloWorldFunction\target\HelloWorld-1.0-shaded.jar
[INFO] Dependency-reduced POM written at: C:\Users\clynch\source\HelloWorldProject\HelloWorldFunction\dependency-reduced-pom.xml
[INFO] --- maven-install-plugin:2.4:install (default-install) @ HelloWorld ---
[INFO] Installing C:\Users\clynch\source\HelloWorldProject\HelloWorldFunction\target\HelloWorld-1.0.jar to C:\Users\clynch\.m2\repository\helloworld\HelloWorld\1.0\HelloWorld-1.0.jar
[INFO] Installing C:\Users\clynch\source\HelloWorldProject\HelloWorldFunction\dependency-reduced-pom.xml to C:\Users\clynch\.m2\repository\helloworld\HelloWorld\1.0\HelloWorld-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.382 s
[INFO] Finished at: 2022-04-04T08:21:35-04:00
[INFO] ------------------------------------------------------------------------

Now we will need to make a Lambda execution role. In the AWS cloud identities, roles and policies control what can and cannot be done. In this case we are going to make a role that has an associated policy that will allow a Lambda to execute in the clound. We will name our role lambda-execution-role. Our next step is to manually configure and upload our lambda to AWS. This will help us understand the more automated way we will see later.

We have successfully configured, uploaded and tested an AWS lambda.

tags: aws - amazon web services - start - kickstart - lambda - s3 - identity acesss management - iam

Less Is More