From c65b3c1fb4580e7a195bf853267d1d4d2f0acdbb Mon Sep 17 00:00:00 2001 From: Carlos Zambrano Date: Wed, 29 Apr 2020 19:51:45 -0500 Subject: [PATCH 01/11] Update template.yml --- template.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template.yml b/template.yml index 4020258..68e5b6f 100644 --- a/template.yml +++ b/template.yml @@ -12,7 +12,7 @@ Resources: Role: !GetAtt LambdaRole.Arn Environment: Variables: - DYNAMO_PLATZI: !ImportValue DynamoPlatzi + DYNAMO_PLATZI: !ImportValue DynamoCurso LambdaRole: Type: AWS::IAM::Role Properties: From b27d5a555e8b359faab29d0a087ab33be4b5c868 Mon Sep 17 00:00:00 2001 From: Carlos Zambrano Date: Wed, 29 Apr 2020 19:57:30 -0500 Subject: [PATCH 02/11] Update lambda_function.py --- lambda_function.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lambda_function.py b/lambda_function.py index 40112c8..50d5428 100644 --- a/lambda_function.py +++ b/lambda_function.py @@ -4,6 +4,7 @@ DYNAMO_PLATZI = os.environ['DYNAMO_PLATZI'] +// hola mama class DynamoAccessor: def __init__(self, dynamo_table): From 7808c166d4f2b9cce263b208c63ed0a16d0af3b6 Mon Sep 17 00:00:00 2001 From: Carlos Zambrano Date: Thu, 30 Apr 2020 19:05:58 -0500 Subject: [PATCH 03/11] Update lambda_function.py remove hola mama --- lambda_function.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lambda_function.py b/lambda_function.py index 50d5428..40112c8 100644 --- a/lambda_function.py +++ b/lambda_function.py @@ -4,7 +4,6 @@ DYNAMO_PLATZI = os.environ['DYNAMO_PLATZI'] -// hola mama class DynamoAccessor: def __init__(self, dynamo_table): From 97b5052dbd733d9fca158e9ff2639eb8e87a96f5 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Sat, 7 Nov 2020 04:11:49 +0000 Subject: [PATCH 04/11] add changes --- dynamo/dynamo.sh | 6 + dynamo/dynamo.yml | 30 +++ lambda_function.py | 10 +- prereq/aws-codepipeline-prereq.sh | 7 + prereq/aws-codepipeline-prereq.yml | 333 +++++++++++++++++++++++++++++ template.yml | 2 +- 6 files changed, 382 insertions(+), 6 deletions(-) create mode 100644 dynamo/dynamo.sh create mode 100644 dynamo/dynamo.yml create mode 100644 prereq/aws-codepipeline-prereq.sh create mode 100644 prereq/aws-codepipeline-prereq.yml diff --git a/dynamo/dynamo.sh b/dynamo/dynamo.sh new file mode 100644 index 0000000..62936d2 --- /dev/null +++ b/dynamo/dynamo.sh @@ -0,0 +1,6 @@ +#!/bin/bash +aws cloudformation deploy \ +--region us-east-1 \ +--parameter-overrides DynamoName="lablambda" DynamoKey="dni" \ +--stack-name dynamo-lambda-lab \ +--template-file ./dynamo.yml \ No newline at end of file diff --git a/dynamo/dynamo.yml b/dynamo/dynamo.yml new file mode 100644 index 0000000..1c0e5ce --- /dev/null +++ b/dynamo/dynamo.yml @@ -0,0 +1,30 @@ +AWSTemplateFormatVersion: '2010-09-09' +Parameters: + DynamoName: + Type: String + DynamoKey: + Type: String + +Resources: + DynamoLab: + Type: AWS::DynamoDB::Table + Properties: + TableName: !Ref DynamoName + BillingMode: PAY_PER_REQUEST + AttributeDefinitions: + - AttributeName: !Ref DynamoKey + AttributeType: S + KeySchema: + - AttributeName: !Ref DynamoKey + KeyType: HASH + SSESpecification: + SSEEnabled: true +Outputs: + DynamoName: + Value: !Ref DynamoLab + Export: + Name: DynamoLab + DynamoNameArn: + Value: !GetAtt DynamoLab.Arn + Export: + Name: DynamoLabArn \ No newline at end of file diff --git a/lambda_function.py b/lambda_function.py index 40112c8..954acb5 100644 --- a/lambda_function.py +++ b/lambda_function.py @@ -2,7 +2,7 @@ import os from boto3.dynamodb.conditions import Key -DYNAMO_PLATZI = os.environ['DYNAMO_PLATZI'] +DYNAMO_PLATZI = os.environ['DYNAMO_BD'] class DynamoAccessor: @@ -10,12 +10,12 @@ def __init__(self, dynamo_table): dynamo_db = boto3.resource('dynamodb') self.table = dynamo_db.Table(dynamo_table) - def get_data_from_dynamo(self, cc): - response = self.table.query(KeyConditionExpression=Key('cc').eq(cc)) + def get_data_from_dynamo(self, dni): + response = self.table.query(KeyConditionExpression=Key('dni').eq(dni)) return response["Items"][0] if any(response["Items"]) else None def lambda_handler(event, context): - dynamo_backend = DynamoAccessor(DYNAMO_PLATZI) - db_element = dynamo_backend.get_data_from_dynamo(event['cc']) + dynamo_backend = DynamoAccessor(DYNAMO_BD) + db_element = dynamo_backend.get_data_from_dynamo(event['dni']) return db_element diff --git a/prereq/aws-codepipeline-prereq.sh b/prereq/aws-codepipeline-prereq.sh new file mode 100644 index 0000000..3c98935 --- /dev/null +++ b/prereq/aws-codepipeline-prereq.sh @@ -0,0 +1,7 @@ +#!/bin/bash +aws cloudformation deploy \ +--region us-east-1 \ +--stack-name codepipeline-lambda-prereq \ +--template-file ./aws-codepipeline-prereq.yml \ +--capabilities CAPABILITY_IAM \ +--capabilities CAPABILITY_NAMED_IAM \ No newline at end of file diff --git a/prereq/aws-codepipeline-prereq.yml b/prereq/aws-codepipeline-prereq.yml new file mode 100644 index 0000000..c0e8798 --- /dev/null +++ b/prereq/aws-codepipeline-prereq.yml @@ -0,0 +1,333 @@ +AWSTemplateFormatVersion: 2010-09-09 +Description: Codepipeline pre requisites + +Resources: + SNSapprovalprd: + Type: AWS::SNS::Topic + Properties: + TopicName: approval-prd + KMSKey: + Type: "AWS::KMS::Key" + Properties: + Enabled: true + KeyPolicy: + Version: "2012-10-17" + Statement: + - Sid: "Enable IAM User Permissions" + Effect: "Allow" + Principal: + AWS: !Sub arn:aws:iam::${AWS::AccountId}:root + Action: + - "kms:*" + Resource: "*" + - Sid: "Allow access for Key Administrators" + Effect: "Allow" + Principal: + AWS: + - !Sub arn:aws:iam::${AWS::AccountId}:root + Action: + - "kms:Create*" + - "kms:Describe*" + - "kms:Enable*" + - "kms:List*" + - "kms:Put*" + - "kms:Update*" + - "kms:Revoke*" + - "kms:Disable*" + - "kms:Get*" + - "kms:Delete*" + - "kms:TagResource" + - "kms:UntagResource" + - "kms:ScheduleKeyDeletion" + - "kms:CancelKeyDeletion" + Resource: "*" + + S3ArtifactsBucket: + Type: AWS::S3::Bucket + Properties: + BucketName: !Sub codepipeline-artifacts-bucket-${AWS::AccountId}-${AWS::Region} + + S3ArtifactsBucketPolicy: + Type: AWS::S3::BucketPolicy + Properties: + Bucket: !Ref S3ArtifactsBucket + PolicyDocument: + Version: "2012-10-17" + Id: SSEAndSSLPolicy + Statement: + - Sid: "" + Effect: Allow + Principal: + AWS: + - !GetAtt CodeBuildRole.Arn + - !GetAtt CodePipelineRole.Arn + Action: + - s3:Get* + - s3:Put* + Resource: !Sub "arn:aws:s3:::${S3ArtifactsBucket}/*" + + CodeBuildRole: + Type: AWS::IAM::Role + Properties: + AssumeRolePolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Principal: + Service: + - codebuild.amazonaws.com + Action: + - sts:AssumeRole + Path: "/service-role/" + Policies: + - PolicyName: codebuild-policy + PolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Action: + - ecr:GetAuthorizationToken + - ec2:CreateNetworkInterface + - ec2:DescribeDhcpOptions + - ec2:DescribeNetworkInterfaces + - ec2:DeleteNetworkInterface + - ec2:DescribeSubnets + - ec2:DescribeSecurityGroups + - ec2:DescribeVpcs + - ec2:CreateNetworkInterfacePermission + - lambda:* + - cloudformation:Describe* + - cloudformation:Get* + - cloudformation:List* + - cloudformation:ValidateTemplate + - cloudformation:ExecuteChangeSet + Resource: "*" + - Effect: Allow + Action: + - s3:* + - sqs:* + - sns:* + Resource: "*" + - Effect: Allow + Action: + - kms:* + Resource: + - !GetAtt KMSKey.Arn + - Effect: Allow + Action: + - logs:CreateLogGroup + - logs:CreateLogStream + - logs:PutLogEvents + Resource: + - !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/* + + CodePipelineRole: + Type: AWS::IAM::Role + Properties: + AssumeRolePolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Principal: + Service: + - codepipeline.amazonaws.com + Action: + - sts:AssumeRole + Path: "/" + Policies: + - PolicyName: codepipeline-policy + PolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Action: + - s3:PutObject + - s3:GetObject + - s3:GetObjectVersion + - s3:GetBucketVersioning + Resource: + - !Sub "arn:aws:s3:::${S3ArtifactsBucket}/*" + - Effect: Allow + Action: + - codebuild:BatchGetBuilds + - codebuild:BatchGetProjects + - codebuild:ListBuilds + - codebuild:ListBuildsForProject + - codebuild:ListConnectedOAuthAccounts + - codebuild:ListProjects + - codebuild:ListRepositories + - codebuild:StartBuild + - codebuild:StopBuild + - sns:Publish + - cloudwatch:* + - lambda:InvokeFunction + - lambda:ListFunctions + - s3:* + Resource: "*" + - Effect: Allow + Action: + - kms:DescribeKey + - kms:GenerateDataKey* + - kms:Encrypt + - kms:ReEncrypt* + - kms:Decrypt + Resource: + - !GetAtt KMSKey.Arn + + LambdaPolicyEC2: + Type: AWS::IAM::ManagedPolicy + Properties: + PolicyDocument: + Version: 2012-10-17 + Statement: + - + Effect: Allow + Action: + - ec2:DisassociateAddress + - ec2:CreateDhcpOptions + - ec2:ReplaceRouteTableAssociation + - ec2:RunScheduledInstances + - ec2:RequestSpotFleet + - ec2:DisableVgwRoutePropagation + - ec2:AssociateVpcCidrBlock + - ec2:ReleaseHosts + - ec2:ReplaceRoute + - ec2:DisassociateVpcCidrBlock + - ec2:DeleteNetworkInterfacePermission + - ec2:DescribeNetworkInterfaces + - ec2:StartInstances + - ec2:DescribeNetworkInterfaceAttribute + - ec2:CreateRoute + - ec2:ReplaceNetworkAclEntry + - ec2:CreateSecurityGroup + - ec2:CreateNetworkAcl + - ec2:DescribeNetworkInterfacePermissions + - ec2:TerminateInstances + - ec2:AcceptVpcEndpointConnections + - ec2:DeleteNetworkInterface + - ec2:CreateRouteTable + - ec2:RunInstances + - ec2:StopInstances + - ec2:DisassociateRouteTable + - ec2:DisassociateSubnetCidrBlock + - ec2:ModifyHosts + - ec2:ReplaceNetworkAclAssociation + - ec2:CreateVolume + - ec2:CreateNetworkInterface + - ec2:DetachVpnGateway + - ec2:CreateDefaultVpc + - ec2:DisassociateIamInstanceProfile + - ec2:CreateVpcEndpoint + - ec2:DisableVpcClassicLink + - ec2:CreateSubnet + Resource: + - "*" + LambdaPolicyDynamo: + Type: AWS::IAM::ManagedPolicy + Properties: + PolicyDocument: + Version: 2012-10-17 + Statement: + - + Effect: Allow + Action: + - dynamodb:PutItem + - dynamodb:UpdateItem + - dynamodb:GetItem + - dynamodb:* + Resource: + - "*" + + LambdaPolicyCW: + Type: AWS::IAM::ManagedPolicy + Properties: + PolicyDocument: + Version: 2012-10-17 + Statement: + - + Effect: Allow + Action: + - logs:* + - cloudwatch:* + - xray:* + Resource: + - "*" + + LambdaRole: + Type: AWS::IAM::Role + Properties: + ManagedPolicyArns: + - !Ref LambdaPolicyDynamo + - !Ref LambdaPolicyEC2 + - !Ref LambdaPolicyCW + AssumeRolePolicyDocument: + Version: 2012-10-17 + Statement: + - + Effect: Allow + Principal: + Service: + - lambda.amazonaws.com + Action: + - sts:AssumeRole + Path: / + LambdaPolicyS3: + Type: AWS::IAM::ManagedPolicy + Properties: + PolicyDocument: + Version: 2012-10-17 + Statement: + - + Effect: Allow + Action: + - s3:ListAllMyBuckets + - s3:HeadBucket + - s3:ListObjects + Resource: + - "*" + - + Effect: Allow + Action: + - s3:* + Resource: + - "*" + + + +Outputs: + PipelineCMKey: + Value: !GetAtt KMSKey.Arn + Export: + Name: PipelineCMKey + PipelineRole: + Value: !GetAtt CodePipelineRole.Arn + Export: + Name: PipelineRole + CodeBuildRole: + Value: !GetAtt CodeBuildRole.Arn + Export: + Name: CodeBuildRole + S3ArtifactsBucket: + Value: !Ref S3ArtifactsBucket + Export: + Name: S3ArtifactsBucket + SNSapprovalprd: + Value: !Ref SNSapprovalprd + Export: + Name: SNSapprovalprd + LambdaPolicyDynamo: + Value: !Ref LambdaPolicyDynamo + Export: + Name: LambdaPolicyDynamo + LambdaPolicyEC2: + Value: !Ref LambdaPolicyEC2 + Export: + Name: LambdaPolicyEC2 + LambdaPolicyCW: + Value: !Ref LambdaPolicyCW + Export: + Name: LambdaPolicyCW + LambdaPolicyS3: + Value: !Ref LambdaPolicyS3 + Export: + Name: LambdaPolicyS3 diff --git a/template.yml b/template.yml index 68e5b6f..ca3c908 100644 --- a/template.yml +++ b/template.yml @@ -12,7 +12,7 @@ Resources: Role: !GetAtt LambdaRole.Arn Environment: Variables: - DYNAMO_PLATZI: !ImportValue DynamoCurso + DYNAMO_BD: !ImportValue DynamoCurso LambdaRole: Type: AWS::IAM::Role Properties: From 3309f154b219ac846020f9d48faea5a6fce972a2 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Sat, 7 Nov 2020 04:37:46 +0000 Subject: [PATCH 05/11] add features --- lambda_function.py | 2 +- template.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lambda_function.py b/lambda_function.py index 954acb5..4f2bb77 100644 --- a/lambda_function.py +++ b/lambda_function.py @@ -2,7 +2,7 @@ import os from boto3.dynamodb.conditions import Key -DYNAMO_PLATZI = os.environ['DYNAMO_BD'] +DYNAMO_BD = os.environ['DYNAMO_BD'] class DynamoAccessor: diff --git a/template.yml b/template.yml index ca3c908..6851e2b 100644 --- a/template.yml +++ b/template.yml @@ -12,7 +12,7 @@ Resources: Role: !GetAtt LambdaRole.Arn Environment: Variables: - DYNAMO_BD: !ImportValue DynamoCurso + DYNAMO_BD: !ImportValue DynamoName LambdaRole: Type: AWS::IAM::Role Properties: From 975af892118dfaf168646f683a4f1d85cbf4d411 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Sat, 7 Nov 2020 04:52:52 +0000 Subject: [PATCH 06/11] add features --- template.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template.yml b/template.yml index 6851e2b..b3255ae 100644 --- a/template.yml +++ b/template.yml @@ -12,7 +12,7 @@ Resources: Role: !GetAtt LambdaRole.Arn Environment: Variables: - DYNAMO_BD: !ImportValue DynamoName + DYNAMO_BD: !ImportValue DynamoLab LambdaRole: Type: AWS::IAM::Role Properties: From ee5e97edcacb7fadded1b893c8f8113b184a7543 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Sat, 7 Nov 2020 05:06:34 +0000 Subject: [PATCH 07/11] add features --- prereq/aws-codepipeline-prereq.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/prereq/aws-codepipeline-prereq.yml b/prereq/aws-codepipeline-prereq.yml index c0e8798..514a3f4 100644 --- a/prereq/aws-codepipeline-prereq.yml +++ b/prereq/aws-codepipeline-prereq.yml @@ -163,6 +163,7 @@ Resources: - lambda:InvokeFunction - lambda:ListFunctions - s3:* + - iam:PassRole Resource: "*" - Effect: Allow Action: From 537037395491dcc95bfeaa0c4d6097217d175c5e Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Sat, 7 Nov 2020 05:12:13 +0000 Subject: [PATCH 08/11] add features --- prereq/aws-codepipeline-prereq.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/prereq/aws-codepipeline-prereq.yml b/prereq/aws-codepipeline-prereq.yml index 514a3f4..888b5c3 100644 --- a/prereq/aws-codepipeline-prereq.yml +++ b/prereq/aws-codepipeline-prereq.yml @@ -164,6 +164,7 @@ Resources: - lambda:ListFunctions - s3:* - iam:PassRole + - cloudformation:DescribeStacks Resource: "*" - Effect: Allow Action: From 296692e3c7c0e2aed6b6f021e8776eee1212e98e Mon Sep 17 00:00:00 2001 From: Carlos Zambrano Date: Sat, 7 Nov 2020 10:04:27 -0500 Subject: [PATCH 09/11] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index cc6beb2..b379eab 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -# Proyecto Platzi - ## Despliegue del proyeto ### Pre-Requisitos From 0723f4517306c9f4e00abfa752500ae64b2de5f3 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Sat, 7 Nov 2020 16:01:21 +0000 Subject: [PATCH 10/11] agregue un codigo a lambda --- lambda_function.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lambda_function.py b/lambda_function.py index 4f2bb77..9c5fc1b 100644 --- a/lambda_function.py +++ b/lambda_function.py @@ -2,8 +2,9 @@ import os from boto3.dynamodb.conditions import Key -DYNAMO_BD = os.environ['DYNAMO_BD'] +# Hola a todos companeros estoy editando el codigo porque soy developer +DYNAMO_BD = os.environ['DYNAMO_BD'] class DynamoAccessor: def __init__(self, dynamo_table): From 8a561d9a8d17eefd675568967e59419b4b0abcde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Acosta?= Date: Mon, 18 Jan 2021 16:00:07 -0500 Subject: [PATCH 11/11] =?UTF-8?q?Cambio=20en=20configuraci=C3=B3n=20de=20L?= =?UTF-8?q?ambda?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/template.yml b/template.yml index b3255ae..42c1928 100644 --- a/template.yml +++ b/template.yml @@ -7,8 +7,8 @@ Resources: FunctionName: VotaNet Handler: lambda_function.lambda_handler Runtime: python3.7 - MemorySize: 256 - Timeout: 300 + MemorySize: 512 + Timeout: 400 Role: !GetAtt LambdaRole.Arn Environment: Variables: