Commit 8c7e779b authored by Carolina Gilabert's avatar Carolina Gilabert

Initial commit: lambda to s3 connection done.

parents
#Internal Mac files
.DS_Store
#Terraform local state folder
.terraform/
#Temp zip of the lambda code
lambda_code.zip
\ No newline at end of file
const S3 = require ('aws-sdk/clients/s3');
const s3Instance = new S3 ();
const listDirectories = params => {
return new Promise ((resolve, reject) => {
const s3params = {
Bucket: 'carolgilabert-object-finder-store',
MaxKeys: 20,
Delimiter: '/',
};
s3Instance.listObjectsV2 (s3params, (err, data) => {
if (err) {
reject (err);
}
resolve (data);
});
});
};
exports.handler = async (event) => {
const objects = await listDirectories();
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify(objects),
};
return response;
};
{
"scripts": {
"build": "rm lambda_code.zip && zip -X lambda_code.zip index.js",
"deploy": "npm run build && terraform apply"
}
}
\ No newline at end of file
#s3 bucket
#lambda
#alb
provider "aws" {
region = "eu-west-1"
profile = "terraform"
}
terraform {
backend "s3" {
bucket = "carolgilabert-terraform-object-finder"
}
}
resource "aws_s3_bucket" "object_store_bucket" {
bucket = "carolgilabert-object-finder-store"
acl = "private"
versioning {
enabled = true
}
}
resource "aws_iam_role" "object_finder_lambda_role" {
name = "object_finder_lambda_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "object_store_policy_attachment" {
role = "${aws_iam_role.object_finder_lambda_role.name}"
policy_arn = "${aws_iam_policy.object_store_bucket_access.arn}"
}
resource "aws_iam_policy" "object_store_bucket_access" {
name = "object_store_policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["${aws_s3_bucket.object_store_bucket.arn}"]
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject"
],
"Resource": ["${aws_s3_bucket.object_store_bucket.arn}/*"]
}
]
}
EOF
}
resource "aws_lambda_function" "object_finder" {
function_name = "ObjectFinder"
description = "Lambda to retrieve S3 objects"
filename = "lambda_code.zip"
source_code_hash = "${base64sha256(file("lambda_code.zip"))}"
handler = "index.handler"
runtime = "nodejs8.10"
timeout = "7"
role = "${aws_iam_role.object_finder_lambda_role.arn}"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment