Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Power of IaC - AWS CloudFormation

Avatar for MG MG
November 01, 2019

The Power of IaC - AWS CloudFormation

This presentation shows how to use the concept of Infrastructure as a Code and AWS CloudFormation to model business problems using serverless components.

Avatar for MG

MG

November 01, 2019
Tweet

More Decks by MG

Other Decks in Programming

Transcript

  1. Michał Górski Cloud Architect Northmill AB After the work: •

    Football • Fantasy Books • Cats • Tea • Traveling [email protected] michal-gorski
  2. Invoicing process • Easy deployment • Slow process - low

    scalability • Server can be overwhelmed • Infrastructure needs to be managed to achieve high availability
  3. Serverless • No server management • Flexible scaling • Pay

    for value • Automated high availability
  4. Serverless architecture by hand • How to deploy changes? •

    How to maintain more environments? • Can we track changes? • Can we automate component testing? • How can we check if the infrastructure is properly implemented? • How can we check security part of the infrastructure?
  5. Infrastructure as a code - Resources FileBucket: Type: AWS::S3::Bucket Properties:

    LifecycleConfiguration: Rules: - ExpirationInDays: 1 Status: Enabled Prefix: to-process/
  6. Infrastructure as a code - Resources ParseFileLambda: Type: AWS::Serverless::Function Properties:

    Handler: Invoicing::Invoicing.Functions.ParseFileLambda::ExecuteAsync CodeUri: bin/Release/netcoreapp2.1/publish Runtime: dotnetcore2.1 MemorySize: 128 Timeout: 180 Environment: Variables: InvoiceQueueUrl: !Ref InvoiceQueue FileBucketName: !Sub file-bucket-${AWS::StackName}
  7. Infrastructure as a code - Resources FileBucket: Type: AWS::S3::Bucket Properties:

    NotificationConfiguration: LambdaConfigurations: - Event: s3:ObjectCreated:* Filter: S3Key: Rules: - Name: prefix Value: to-process/ Function: !GetAtt ParseFileLambda.Arn
  8. Infrastructure as a code - Resources HandleInvoiceLambda: Type: AWS::Serverless::Function Properties:

    Handler: Invoicing::Invoicing.Functions.HandleInvoiceLambda::ExecuteAsync CodeUri: bin/Release/netcoreapp2.1/publish Runtime: dotnetcore2.1 MemorySize: 128 Timeout: 180 Events: InvoiceQueueEvent: Type: SQS Properties: BatchSize: 1 Queue: !GetAtt InvoiceQueue.Arn
  9. Infrastructure as a code - conclusions • Infrastructure in code

    repository ◦ Change tracking available ◦ Code review possible • Very friendly CI/CD process ◦ Deploy environment ◦ Automatic Component tests ◦ Automatic template validation ◦ Automatic security checks • Why every developer should know the infrastructure?
  10. Infrastructure as a code - conclusions • Infrastructure in code

    repository ◦ Change tracking available ◦ Code review possible • Very friendly CI/CD process ◦ Deploy environment ◦ Automatic Component tests ◦ Automatic template validation ◦ Automatic security checks • Why every developer should know the infrastructure?
  11. Infrastructure as a code - Resources KMSKey: Type: AWS::KMS::Key Properties:

    Description: Invoicing KMS Key KeyPolicy: Version: 2012-10-17 Id: !Sub ${AWS::StackName}-kms-key Statement: - Sid: Allow all Effect: Allow Principal: AWS: !Sub arn:aws:iam::${AWS::AccountId}:root Action: - kms:* Resource: '*'
  12. Infrastructure as a code - Resources FileBucket: Type: AWS::S3::Bucket Properties:

    BucketEncryption: ServerSideEncryptionConfiguration: - ServerSideEncryptionByDefault: SSEAlgorithm: aws:kms KMSMasterKeyID: !Ref KMSKey
  13. More Functions !Join !Join [ ":", [ a, b, c

    ] ] a:b:c !Split !Split [ "|" , "a||c|" ] ["a", "", "c", ""] !Select !Select [ "1", [ "google", "aws", "azure"] ] aws
  14. Infrastructure as a code - Resources FileBucketPermission: Type: AWS::Lambda::Permission Properties:

    Action: lambda:InvokeFunction FunctionName: !GetAtt ParseFileLambda.Arn Principal: s3.amazonaws.com SourceArn: !GetAtt FileBucket.Arn
  15. Circular dependency problem FileBucket: Type: AWS::S3::Bucket Properties: NotificationConfiguration: LambdaConfigurations: -

    Event: s3:ObjectCreated:* Filter: S3Key: Rules: - Name: prefix Value: to-process/ Function: !GetAtt ParseFileLambda.Arn
  16. Circular dependency problem FileBucketPermission: Type: AWS::Lambda::Permission Properties: Action: lambda:InvokeFunction FunctionName:

    !GetAtt ParseFileLambda.Arn Principal: s3.amazonaws.com SourceArn: !GetAtt FileBucket.Arn
  17. Circular dependency problem FileBucketPermission: Type: AWS::Lambda::Permission Properties: Action: lambda:InvokeFunction FunctionName:

    !GetAtt ParseFileLambda.Arn Principal: s3.amazonaws.com SourceArn: !Sub arn:aws:s3:::file-bucket-${AWS::StackName}
  18. Infrastructure as a code - Resources ParseFileLambdaRole: Type: AWS::IAM::Role Properties:

    Policies: - PolicyName: allowGetObjectFromS3 PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - s3:GetObject Resource: !Sub arn:aws:s3:::file-bucket-${AWS::StackName}/to-process/*
  19. Infrastructure as a code - Resources ParseFileLambdaRole: Type: AWS::IAM::Role Properties:

    Policies: - PolicyName: allowDecryptFiles PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - kms:Decrypt Resource: !GetAtt KMSKey.Arn
  20. Infrastructure as a code - Resources ParseFileLambdaRole: Type: AWS::IAM::Role Properties:

    ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - sts:AssumeRole Principal: Service: - lambda.amazonaws.com
  21. Infrastructure as a code - Resources HandleInvoiceLambdaRole: Type: AWS::IAM::Role Properties:

    Policies: - PolicyName: allowPutObjectToS3 PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - s3:PutObject Resource: !Sub arn:aws:s3:::file-bucket-${AWS::StackName}/invoices/*
  22. Infrastructure as a code - Resources HandleInvoiceLambdaRole: Type: AWS::IAM::Role Properties:

    Policies: - PolicyName: allowToUseSqs PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - sqs:ReceiveMessage - sqs:DeleteMessage - sqs:GetQueueAttributes Resource: !GetAtt InvoiceQueue.Arn
  23. Infrastructure as a code - Resources InvoiceQueue: Type: AWS::SQS::Queue Properties:

    VisibilityTimeout: 180 RedrivePolicy: deadLetterTargetArn: !GetAtt InvoiceDeadLetterQueue.Arn maxReceiveCount: 3
  24. Infrastructure as a code - Outputs Outputs: PutObjectsInBucketPolicy: Value: !Ref

    PutObjectsInBucketPolicy Export: Name: !Sub PutObjectsInFileBucketPolicy-${AWS::StackName}
  25. Infrastructure as a code - Outputs PutObjectsInBucketPolicy: Type: AWS::IAM::ManagedPolicy Properties:

    Description: Policy for uploading files to file bucket PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: s3:PutObject Resource: !Sub ${FileBucket.Arn}/to-process/* - Effect: Allow Action: kms:GenerateDataKey* Resource: !GetAtt KMSKey.Arn
  26. Infrastructure as a code - AWS CLI dotnet lambda deploy-serverless

    -cfg deploy-config-dev.json { "profile": "Default", "region": "eu-west-1", "configuration": "Release", "framework": "netcoreapp2.1", "s3-bucket": "sandbox-deploy-bucket", "s3-prefix": "Invoicing/", "template": "serverless.yaml", "stack-name": "invoicing-dev", "stack-wait": true }
  27. Infrastructure as a code - conclusions • Infrastructure in code

    repository ◦ Change tracking available ◦ Code review possible • Very friendly CI/CD process ◦ Deploy environment ◦ Automatic Component tests - on demand environment ◦ Automatic template validation ◦ Automatic security checks • Why every developer should know the infrastructure?
  28. Infrastructure as a code - AWS CLI dotnet lambda deploy-serverless

    -cfg deploy-config-dev.json { "profile": "Default", "region": "eu-west-1", "configuration": "Release", "framework": "netcoreapp2.1", "s3-bucket": "sandbox-deploy-bucket", "s3-prefix": "Invoicing/", "template": "serverless.yaml", "stack-name": "invoicing-dev", "stack-wait": true }
  29. Infrastructure as a code - Resources KMSKey: Type: AWS::KMS::Key Properties:

    Description: Invoicing KMS Key KeyPolicy: Version: 2012-10-17 Id: !Sub ${AWS::StackName}-kms-key Statement: - Sid: Allow all Effect: Allow Principal: AWS: !Sub arn:aws:iam::${AWS::AccountId}:root Action: - kms:* Resource: '*'