Menu
myezbrew
  • Privacy Policy
  • prometheus documentation
myezbrew

Lambda crash course…

Posted on December 4, 2021December 6, 2021 by Andy Jenkins
Follow us on Social Media
linkedin

Creating a Role

  • Create Policy using AWS Policy Generator
    • Type -> IAM Policy
    • AWS Service -> Amazon SES
    • Actions -> SendEmail, SendRawEmail
    • ARN -> *
    •  Click Add Statement
    • Click Generate Policy
    • Copy and save the text
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1638644864547",
      "Action": [
        "ses:SendEmail",
        "ses:SendRawEmail"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
  • Create Role
    • AWS Service -> Lambda -> Next: Permissions
    • Create role
    • Click Click Create policy -> json -> paste policy from above
      • Click next: Tags
      • Click next: Review
      • Click next: Create
    • Attach policy to role – > Click next: Tags -> Click next: Review
    • Role Name: testrole – > Click: Create

Create first lambda – “lambda canary”

  • Create a function from a blueprint
    • AWS Lambda page -> Functions -> Create Function
    • Select “Use a blueprint”
    • Search for “canary”
    • Select lambda-canary
    • Click: Configure
    • Function name: lambda-canary
    • Configure CloudWatch Events trigger
      • Create a new rule
      • Rule name: canary
      • Rule type -> Schedule expression -> rate(5 minutes)
      • Leave Defaults -> Create
      • Configuration
        • expected: myezbrew
        • site: https://blog.myezbrew.com/
        • Save
      • Test
        • Name: testevent1
        • Click Save
        • Click Test
  • Configure CloudWatch schedule
    • CloudWatch -> Alarms -> Create alarm
    • Select Metric
      • Search canary
      • Select canary errors
      • Graphed metrics -> Statistic: Sum
    • Notification
      • Select: Create new topic
      • Email: wasdx@wasdx
      • Click: Create topic
      • Click Next
      • Name -> canaryAlarm -> Next
    • Preview and create
      • Click: Create alarm
    •  Test it
Alarm Details:
- Name:                       canaryAlarm
- Description:                canaryAlarm
- State Change:               OK -> ALARM
- Reason for State Change:    Threshold Crossed: 1 out of the last 1 datapoints [1.0 (04/12/21 23:11:00)] was greater than the threshold (0.0) (minimum 1 datapoint for OK -> ALARM transition).
- Timestamp:                  Saturday 04 December, 2021 23:16:22 UTC
- AWS Account:                932030202108
- Alarm Arn:                  arn:aws:cloudwatch:us-west-2:932030202108:alarm:canaryAlarm 

hello world! 

Lambda twitter bot

  • Twitter developer account
    • API keys and what not
  • AWS Parameter Store
    • Click: Create parameter
      • Name: CONSUMER_KEY
      • Type: SecureString
      • Value: Twitter API Key
    • Create the rest…
      • AWS
        • ACCESS_TOKEN_KEY
        • ACCESS_TOKEN_SECRET
      • Twitter
        • CONSUMER_KEY
        • CONSUMER_SECRET
  • Some local python setup:
    • python3 -m venv .venv
    • ..venvScriptsactivate
    • pip3 install twython
    • pip3 install boto3
    • pip freeze
(.venv) PS C:Usersvolksrepointroduction-aws-lambda-masterm4sparrow.venvScripts> pip freeze
boto3==1.20.20
botocore==1.23.20
certifi==2021.10.8
charset-normalizer==2.0.9
idna==3.3
jmespath==0.10.0
oauthlib==3.1.1
python-dateutil==2.8.2
requests==2.26.0
requests-oauthlib==1.3.0
s3transfer==0.5.0
six==1.16.0
twython==3.9.1
urllib3==1.26.7
    • python (open the interpreter)
    • … load what you need …
    • send_tweet(‘Then to divert your small mind from your unfortunate predicament, I shall tell you an amusing anecdote. @myezbrew’)
  •  Build a package to deploy
    • Nuke the old .venv file use for testing above.
    • python3 -m venv .venv
    • ..venvScriptsactivate
    • mkdir setup
    • cp .sparrow.py .setup
    • cp .ssm_secrets.py .setup
    • cd .setup
    • pip install -r ..requirements.txt -t .
    • Compress-Archive -Path .* -DestinationPath ..package.zip
    • deactivate

(.venv) PS C:Usersvolksrepointroduction-aws-lambda-masterm4sparrow> cp .sparrow.py .setup
(.venv) PS C:Usersvolksrepointroduction-aws-lambda-masterm4sparrow> cp .ssm_secrets.py .setup
(.venv) PS C:Usersvolksrepointroduction-aws-lambda-masterm4sparrow> cd setup
(.venv) PS C:Usersvolksrepointroduction-aws-lambda-masterm4sparrowsetup> pip install -r ..requirements.txt -t .
….

(.venv) PS C:Usersvolksrepointroduction-aws-lambda-masterm4sparrowsetup> Compress-Archive -Path .* -DestinationPath ..package.zip

  • AWS -> Lambda -> Create function
    • Autor from scratch
    • Function name: sparrow
    • Runtime: Python 3.7
    • Create a new role from AWS policy templates
      • Name: sparrowRole
    • Create function
    • Configuration -> permissions Role name -> Click: sparrowRole
      • kms
      • SSMReadOnly
    • Add trigger
      • EventBridge
        • Rule name: dailyTweet
        • Rule Type -> Scheduled expression
        • Scheduled Expression* -> rate(1 day)
        • Click: Add
    • Upload the package
      • Code -> Upload from -> Computer
      • Select package.zip
      • Click: Save
    • Edit the handler (launcher)
      • Runtime settings -> Edit
        • Handler -> sparrow.handler
      • Click: Test
        • Event Template: cloudwatch-scheduled-event
        • Event name: dailytest
        • Click: Create
        • Click Test
  • qwer
  • wsert

Integrating other AWS Services with Lambda….

 

Recent Posts

  • HAOS – Notes
  • Vger: Docker Images
  • Project: Duplicate Terraform Cloud Global Variable
  • PowerShell Crash Day
  • vger: Using a man in the middle terraform module

Recent Comments

    Archives

    • October 2023
    • September 2023
    • August 2023
    • March 2023
    • February 2023
    • November 2022
    • May 2022
    • April 2022
    • December 2021
    • October 2021
    • September 2021

    Categories

    • devops
    • docker images
    • prometheus
    • Prometheus Alerting
    • Uncategorized
    • vger

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org
    ©2025 myezbrew | WordPress Theme by Superb Themes