Skip to content

Instantly share code, notes, and snippets.

@FredrikMeyer
Created February 23, 2022 20:44
Show Gist options
  • Save FredrikMeyer/840d7972b9d197519fa1b2a151ec0a4b to your computer and use it in GitHub Desktop.
Save FredrikMeyer/840d7972b9d197519fa1b2a151ec0a4b to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation template for a S3 bucket
Resources:
MyS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: i-named-this-bucket-1337
MyLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: "my-function"
Handler: index.handler
Code:
ZipFile: |
var AWS = require("aws-sdk");
var s3 = new AWS.S3();
exports.handler = function(event, context) {
s3.listObjects({ Bucket: process.env.S3_BUCKET},
function (err, data) {
if (err) throw err;
console.log(data)
})
console.log("I'm a lambda!")
};
Runtime: nodejs14.x
Role: !GetAtt LambdaExecutionRole.Arn
Environment:
Variables:
S3_BUCKET: !Ref MyS3Bucket
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: root
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:*
Resource: arn:aws:logs:*:*:*
- PolicyName: list-s3
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:List*
Resource: !GetAtt MyS3Bucket.Arn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment