From d577cd02ef6d1dbb8edfd7fa2887f94ddd6e9af9 Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Thu, 14 May 2020 04:39:51 -0400 Subject: [PATCH] Create infrastructure to call lambda --- .gitignore | 5 +- .../cloudformation/dt/cloudwatch.yaml | 39 +++++++++++ infrastructure/cloudformation/dt/iam.yaml | 40 ++++++++++++ infrastructure/cloudformation/dt/lambdas.yaml | 64 +++++++++++++++++++ infrastructure/cloudformation/dt/top.yaml | 35 ++++++++++ .../task_queue_manager/lambda_handler.py | 4 ++ 6 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 infrastructure/cloudformation/dt/cloudwatch.yaml create mode 100644 infrastructure/cloudformation/dt/iam.yaml create mode 100644 infrastructure/cloudformation/dt/lambdas.yaml create mode 100644 infrastructure/lambda/task_queue_manager/lambda_handler.py diff --git a/.gitignore b/.gitignore index 7db8a2f..e1289ef 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,7 @@ *.exe *.out *.app -.vscode \ No newline at end of file +.vscode + +# Compressed Artifacts +*.zip diff --git a/infrastructure/cloudformation/dt/cloudwatch.yaml b/infrastructure/cloudformation/dt/cloudwatch.yaml new file mode 100644 index 0000000..34ecf77 --- /dev/null +++ b/infrastructure/cloudformation/dt/cloudwatch.yaml @@ -0,0 +1,39 @@ +AWSTemplateFormatVersion: "2010-09-09" +Description: DT CloudWatch stack +Parameters: + #------------------------ + # Deployment Information + #------------------------ + environment: + Type: String + Description: Name of the environment + Default: production + + #---------------- + # ECS Information + #---------------- + Cluster: + Description: The ECS cluster to watch + Type: String + + #------------------- + # Lambda Information + #------------------- + LambdaArn: + Description: Lambda function to call upon ecs task state change + Type: String + +Resources: + + TaskListRule: + EventPattern: + source: + - "aws.ecs" + detail-type: + - "ECS Task State Change" + detail: + clusterArn: + - !Ref Cluster + Targets: + - Id: RedisUpdater + Arn: !Ref LambdaArn \ No newline at end of file diff --git a/infrastructure/cloudformation/dt/iam.yaml b/infrastructure/cloudformation/dt/iam.yaml new file mode 100644 index 0000000..7aea6e3 --- /dev/null +++ b/infrastructure/cloudformation/dt/iam.yaml @@ -0,0 +1,40 @@ +AWSTemplateFormatVersion: "2010-09-09" +Description: DT IAM stack +Parameters: + #------------------------ + # Deployment Information + #------------------------ + environment: + Type: String + Description: Name of the environment + Default: production + +Resources: + DefaultLambdaRole: + Type: AWS::IAM::Role + Properties: + AssumeRolePolicyDocument: + Version: 2012-10-17 + Statement: + - Effect: Allow + Principal: + Service: + - lambda.amazonaws.com + Action: + - sts:AssumeRole + Policies: + - PolicyName: LambdaLogging + PolicyDocument: + Version: 2012-10-17 + Statement: + - Effect: Allow + Action: + - logs:CreateLogGroup + - logs:CreateLogStream + - logs:PutLogEvents + Resource: "*" + +Outputs: + DefaultRole: + Description: Default lambda role with logging policy + Value: !Ref DefaultLambdaRole \ No newline at end of file diff --git a/infrastructure/cloudformation/dt/lambdas.yaml b/infrastructure/cloudformation/dt/lambdas.yaml new file mode 100644 index 0000000..899a16b --- /dev/null +++ b/infrastructure/cloudformation/dt/lambdas.yaml @@ -0,0 +1,64 @@ +AWSTemplateFormatVersion: "2010-09-09" +Description: DT Lambdas stack +Parameters: + #------------------------ + # Deployment Information + #------------------------ + environment: + Type: String + Description: Name of the environment + Default: production + + #---------------- + # IAM Information + #---------------- + TaskManagerRole: + Type: String + Description: IAM role assumed by Task Manager Lambda + VpcId: + Type: AWS::EC2::VPC::Id + Description: The id of the VPC the cluster will be in + ConstraintDescription: VPC Id must begin with 'vpc-' + SubnetIds: + Type: List + Description: Comma seperated list of subnets for ECS instances to run in + +Resources: + + TaskListSecurityGroup: + Type: AWS::EC2::SecurityGroup + Properties: + GroupDescription: TaskListManagerLambda Allowed Ports + VpcId: !Ref VpcId + SecurityGroupIngress: + - IpProtocol: tcp + FromPort: '0' + ToPort: '65535' + CidrIp: 0.0.0.0/0 + SecurityGroupEgress: + - IpProtocol: tcp + FromPort: '0' + ToPort: '65535' + CidrIp: 0.0.0.0/0 + + + TaskListManagerLambda: + Type: AWS::Lambda::Function + Runtime: python3.7 + Code: + S3Bucket: sumu-stacks + S3Key: !Sub "dt/${release}/lambda/task_queue_manager.zip" + FunctionName: !Sub "FnQueueManager-DT-${environment}" + Description: + MemorySize: 128 + Timeout: 10 + Role: !Ref QueueManagerRole + VpcConfig: + SecurityGroupIds: + - !Ref TaskListSecurityGroup + SubnetIds: !Ref SubnetIds + +Outputs: + TaskListManager: + Value: !Ref TaskListManagerLambda + Description: Function that adds and removes tasks from a redis list \ No newline at end of file diff --git a/infrastructure/cloudformation/dt/top.yaml b/infrastructure/cloudformation/dt/top.yaml index ca21e92..d1d0c34 100644 --- a/infrastructure/cloudformation/dt/top.yaml +++ b/infrastructure/cloudformation/dt/top.yaml @@ -68,6 +68,41 @@ Resources: SubDomain: !Ref SubDomain DtDNS: !GetAtt LoadBalancing.Outputs.NlbDnsName + #----- + # IAM + #----- + IAM: + Type: AWS::CloudFormation::Stack + Properties: + TemplateURL: !Sub 'https://s3.${AWS::Region}.amazonaws.com/sumu-stacks/dt/${release}/cloudformation/dt/iam.yaml' + Parameters: + environment: !Ref environment + + #-------- + # Lambda + #-------- + LambdaFunctions: + Type: AWS::CloudFormation::Stack + Properties: + TemplateURL: !Sub 'https://s3.${AWS::Region}.amazonaws.com/sumu-stacks/dt/${release}/cloudformation/dt/lambda.yaml' + Paramters: + environment: !Ref environment + TaskManagerRole: !GetAtt IAM.Outputs.DefaultRole + VpcId: !Ref VpcId + SubnetIds: !Ref PublicSubnets + + #------------ + # CloudWatch + #------------ + CloudWatchRules: + Type: AWS::CloudFormation::Stack + Properties: + TemplateURL: !Sub 'https://s3.${AWS::Region}.amazonaws.com/sumu-stacks/dt/${release}/cloudformation/dt/cloudwatch.yaml' + Paramters: + environment: !Ref environment + Cluster: !GetAtt EcsCluster.Outputs.Cluster + LambdaArn: !GetAtt LambdaFunctions.Outputs.TaskListManager + #--------- # Caching #--------- diff --git a/infrastructure/lambda/task_queue_manager/lambda_handler.py b/infrastructure/lambda/task_queue_manager/lambda_handler.py new file mode 100644 index 0000000..ddaa514 --- /dev/null +++ b/infrastructure/lambda/task_queue_manager/lambda_handler.py @@ -0,0 +1,4 @@ +import json + +def lambda_handler(event, context): + print(json.dumps(event)) \ No newline at end of file