Skip to content

Commit

Permalink
add stacks for raw sync lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
philerooski committed Sep 18, 2024
1 parent 27ca5db commit 51420b9
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
15 changes: 15 additions & 0 deletions config/develop/namespaced/lambda-raw-sync-role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
template:
path: lambda-raw-role.yaml
stack_name: "{{ stack_group_config.namespace }}-lambda-raw-role"
dependencies:
- develop/namespaced/sns-dispatch.yaml
- develop/namespaced/sqs-input-to-dispatch.yaml
- develop/s3-cloudformation-bucket.yaml
- develop/s3-input-bucket.yaml
- develop/s3-raw-bucket.yaml
parameters:
S3SourceBucketName: {{ stack_group_config.input_bucket_name }}
S3TargetBucketName: {{ stack_group_config.raw_bucket_name }}
SNSTopicArn: !stack_output_external "{{ stack_group_config.namespace }}-sns-dispatch::SnsTopicArn"
stack_tags:
{{ stack_group_config.default_stack_tags }}
18 changes: 18 additions & 0 deletions config/develop/namespaced/lambda-raw-sync.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
template:
type: sam
path: src/lambda_function/raw_sync/template.yaml
artifact_bucket_name: {{ stack_group_config.template_bucket_name }}
artifact_prefix: "{{ stack_group_config.namespace }}/src/lambda"
dependencies:
- develop/namespaced/lambda-raw-sync-role.yaml
- develop/s3-cloudformation-bucket.yaml
- develop/s3-raw-bucket.yaml
- develop/s3-input-bucket.yaml
stack_name: "{{ stack_group_config.namespace }}-lambda-raw-sync"
parameters:
RoleArn: !stack_output_external "{{ stack_group_config.namespace }}-lambda-raw-sync-role::RoleArn"
S3InputBucket: {{ stack_group_config.input_bucket_name }}
S3InputKeyPrefix: "{{ stack_group_config.namespace }}/"
S3RawBucket: {{ stack_group_config.raw_bucket_name }}
S3RawKeyPrefix: "{{ stack_group_config.namespace }}/json/"
stack_tags: {{ stack_group_config.default_stack_tags }}
60 changes: 60 additions & 0 deletions src/lambda_function/raw_sync/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Description: >
SAM Template for the raw sync Lambda. The raw sync Lambda ensures that
the input and raw S3 buckets are synchronized by verifying that all non-zero
sized JSON in the exports in the input bucket have a corresponding object in
the raw bucket.
Parameters:

RoleArn:
Type: String
Description: ARN of the raw sync Lambda role.

S3InputBucket:
Type: String
Description: Name of the input S3 bucket.

S3InputKeyPrefix:
Type: String
Description: S3 key prefix where exports are written.

S3RawBucket:
Type: String
Description: Name of the Raw S3 bucket.

S3RawKeyPrefix:
Type: String
Description: S3 key prefix where files are written.

LambdaPythonVersion:
Type: String
Description: Python version to use for this lambda function
Default: "3.9"

Resources:
RawSyncFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Zip
CodeUri: ./
Handler: app.lambda_handler
Runtime: !Sub "python${LambdaPythonVersion}"
Role: !Ref RoleArn
MemorySize: 1024
Timeout: 900
Environment:
Variables:
INPUT_S3_BUCKET: !Ref S3InputBucket
INPUT_S3_KEY_PREFIX: !Ref S3InputKeyPrefix
RAW_S3_BUCKET: !Ref S3RawBucket
RAW_S3_KEY_PREFIX: !Ref S3RawKeyPrefix

Outputs:
RawSyncFunctionArn:
Description: Arn of the raw sync Lambda.
Value: !GetAtt RawSyncFunction.Arn
Export:
Name: !Sub "${AWS::Region}-${AWS::StackName}-RawSyncFunctionArn"
71 changes: 71 additions & 0 deletions templates/lambda-raw-sync-role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
AWSTemplateFormatVersion: '2010-09-09'

Transform: AWS::Serverless-2016-10-31

Description: >
An IAM Role for the raw sync Lambda
Parameters:
S3SourceBucketName:
Type: String
Description: Name of the S3 bucket where exports are deposited.

S3TargetBucketName:
Type: String
Description: Name of the S3 bucket where raw JSON is written to.

SNSTopicArn:
Type: String
Description: >
ARN of the SNS topic where files found not to have a corresponding
object in the target bucket will be published to for processing.
Resources:
RawRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: ReadS3
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:Get*
- s3:List*
Resource:
- !Sub arn:aws:s3:::${S3SourceBucketName}
- !Sub arn:aws:s3:::${S3SourceBucketName}/*
- !Sub arn:aws:s3:::${S3TargetBucketName}
- !Sub arn:aws:s3:::${S3TargetBucketName}/*
- PolicyName: PublishToSNS
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- sns:Publish
Resource:
- !Ref SNSTopicArn

Outputs:
RoleName:
Value: !Ref RawRole
Export:
Name: !Sub '${AWS::Region}-${AWS::StackName}-RoleName'

RoleArn:
Value: !GetAtt RawRole.Arn
Export:
Name: !Sub '${AWS::Region}-${AWS::StackName}-RoleArn'

0 comments on commit 51420b9

Please sign in to comment.