Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active January 19, 2021 01:25
Show Gist options
  • Save rpivo/1123bcaaa48a4fbacc6d06dee284ea3f to your computer and use it in GitHub Desktop.
Save rpivo/1123bcaaa48a4fbacc6d06dee284ea3f to your computer and use it in GitHub Desktop.
A Simple CloudFormation Template

A Simple CloudFormation Template

Basic keys:

AWSTemplateFormatVersion: "2010-09-09" # AWS Template Format Version will always be 2010-09-09 until AWS updates the format version
Description: a sample template # Description: string
Resources: # list of varioues types of resources, including Lambda functions & layers, EC2 instances, S3 buckets, IAM execution roles, etc
  SomeResource: # user-given ID (name) for the resource
    Type: "AWS::EC2::Instance" # type of the resource. See AWS resource and property types reference in references
    Properties: # properties that are unique to the resource type. See AWS resource and property types reference in references for a list of available properties on each resource type

Without comments:

AWSTemplateFormatVersion: "2010-09-09"
Description: a sample template
Resources:
  SomeResource:
    Type: "AWS::EC2::Instance"
    Properties:

Example from AWS docs with properties specific to the resource type:

AWSTemplateFormatVersion: "2010-09-09"
Description: A sample template
Resources:
  MyEC2Instance: #An inline comment
    Type: "AWS::EC2::Instance"
    Properties: 
      ImageId: "ami-0ff8a91507f77f867" #Another comment -- This is a Linux AMI
      InstanceType: t2.micro
      KeyName: testkey
      BlockDeviceMappings:
        -
          DeviceName: /dev/sdm
          Ebs:
            VolumeType: io1
            Iops: 200
            DeleteOnTermination: false
            VolumeSize: 20

An expanded example with a few different resource types, including a Lambda layer (a .zip file archive that contains libraries, a custom runtime, or other dependencies), an S3 bucket, an IAM execution role, and a Lambda function, as well as some outputs.

AWSTemplateFormatVersion: '2010-09-09'
Description: 'Stack to setup environment for Textract-Comprehend workshop'

Resources:

  ElasticLibs:
    Type: "AWS::Lambda::LayerVersion"
    Properties:
      CompatibleRuntimes: 
        - python3.7
      Content: 
        S3Bucket: !Join
        - "-"
        - - "aws-textract-workshop"
          - !Ref 'AWS::Region'
        S3Key: bootstrap/elasticlibs.zip
      Description: Layer for elasticsearch libraries (elasticsearch, aws-requests-auth, requests-aws4auth)
      LayerName: ElasticLibs
      LicenseInfo: Apache2


  InputS3Bucket:
    Type: AWS::S3::Bucket
    Properties: 
       BucketName: !Join
        - "-"
        - - "workshop-textract"
          - !Select [0, !Split ["-", !Select [2, !Split ["/", !Ref "AWS::StackId"]]]]

  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: "/"
      ManagedPolicyArns:
      - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
      Policies:
      - PolicyName: s3policy
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Effect: Allow
            Action:
            - s3:get*
            Resource: !Join 
              - ''
              - - 'arn:aws:s3:::'
                - !Ref InputS3Bucket
                - /*
      - PolicyName: textractpolicy
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Effect: Allow
            Action:
            - textract:*
            Resource: "*"

  DocumentTextract:
    Type: AWS::Lambda::Function
    Properties:
      Description: Lambda function in charge of launching Textract
      FunctionName: !Join
        - "-"
        - - "documentTextract"
          - !Select [0, !Split ["-", !Select [2, !Split ["/", !Ref "AWS::StackId"]]]]
      Handler: index.handler
      MemorySize: 128
      Role: !GetAtt LambdaExecutionRole.Arn
      Runtime: python3.7
      Timeout: 300
      Code:
        ZipFile: |
          import json
          def handler(event, context):
            # TODO: Replace with code to call textract
          	response = {
          		'statusCode': 200,
          		'body': json.dumps('Hello!')
          	}
          	return response

Outputs:
  InputS3Bucket:
    Description: Name of the input bucket
    Value: !Ref InputS3Bucket
  DocumentTextractFunction:
    Description: Function that will be triggered when a document is uploaded in the input bucket
    Value: !Ref DocumentTextract

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment