GithubHelp home page GithubHelp logo

haukurh / myntbreyta.is-currencies-lambda Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 68 KB

A small AWS Lambda function, which fetches currency rates and stores it in a S3 bucket

License: MIT License

JavaScript 100.00%
aws lambda s3 myntbreyta-is

myntbreyta.is-currencies-lambda's Introduction

myntbreyta.is-currencies-lambda

A small AWS lambda function that fetches currency rates in XML format from a credit-card vendor in Iceland, parses the data then stores it in an AWS S3 bucket for myntbreyta.is to use.

ToDo

  • Validate env variables before starting

Prerequisite

Create a AWS lambda function

Here I'm following the AWS documentation and adjusting the commands to add the necessary permissions and variables.

Create a AWS IAM role with a trust relationship with lambda

aws iam create-role --role-name myntbreyta-is-currencies-lambda --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}'

Give the role 'AWSLambdaBasicExecutionRole' permission policy

aws iam attach-role-policy --role-name myntbreyta-is-currencies-lambda --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

Add a custom policy to the role to allow the lambda to update the object in our S3 and create a CloudFront invalidation

aws iam put-role-policy --role-name myntbreyta-is-currencies-lambda --policy-name allow-myntbreyta-s3-upload --policy-document '{"Version":"2012-10-17","Statement":[{"Action":["s3:PutObject","s3:GetObject","s3:ListBucket","s3:DeleteObject"],"Effect":"Allow","Resource":"arn:aws:s3:::<S3_BUCKET_ID>/currency-rates.json"}]}'

Prepare the code to create our new lambda

yarn install --production
zip -r myntbreyta-is-currencies-lambda.zip . -x ".git/*" -x ".idea/*"

Create the lambda using the role we just created

aws lambda create-function --function-name myntbreyta-is-currencies-lambda \
    --environment '{"Variables":{"xml_url":"<XML_URL>","bucket":"<S3_BUCKET_ID>"}}' \
    --zip-file fileb://myntbreyta-is-currencies-lambda.zip \
    --handler index.handler \
    --runtime nodejs20.x \
    --timeout 10 \
    --memory-size 512 \
    --role arn:aws:iam::<AWS_ACCOUNT_ID>:role/myntbreyta-is-currencies-lambda

Test that everything works

aws lambda invoke --function-name myntbreyta-is-currencies-lambda --log-type Tail --query 'LogResult' --output text /dev/null | base64 -d

Creating a scheduled event with EventBridge

aws events put-rule --name myntbreyta-is-currencies-lambda-scheduled-rule --schedule-expression 'cron(45 06 * * ? *)'
aws lambda add-permission \
    --function-name myntbreyta-is-currencies-lambda \
    --statement-id myntbreyta-is-currencies-lambda-scheduled-event \
    --action 'lambda:InvokeFunction' \
    --principal events.amazonaws.com \
    --source-arn arn:aws:events:eu-west-1:<AWS_ACCOUNT_ID>:rule/myntbreyta-is-currencies-lambda-scheduled-rule
aws events put-targets --rule myntbreyta-is-currencies-lambda-scheduled-rule --targets '[{"Id": "1","Arn": "arn:aws:lambda:eu-west-1:<AWS_ACCOUNT_ID>:function:myntbreyta-is-currencies-lambda"}]'

Sources

myntbreyta.is-currencies-lambda's People

Contributors

haukurh avatar

Watchers

 avatar

myntbreyta.is-currencies-lambda's Issues

CloudFront doesn't listen to Expire header

CloudFront doen't listen to the Expire header because Cache-Control is also specifed

We recommend that you use the Cache-Control max-age directive instead of the Expires header field to control object caching. If you specify values both for Cache-Control max-age and for Expires, CloudFront uses only the value of Cache-Control max-age.

Source

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.