简体   繁体   中英

How to property configure AWS CloudFormation template to run containers of Fargate?

I'm following this excellent blog post on hosting a severless Scrapy application on AWS Fargate. Here's my tree structure thus far:

.
├── Dockerfile
├── Makefile
├── fargate-template.yml
├── handler.py
├── launch_fargate.py
├── launcher.py
├── my_sls_scraper
├── node_modules
├── package-lock.json
├── package.json
├── requirements.txt
├── s3-template.yml
├── scrapy.cfg
└── serverless.yml

However, deploying using sls deploy -v gives me the following error: Unable to resolve '3' log group name out of: {RetentionInDays: 1} .

Here's the full error:

Serverless: Configuration warning:
Serverless:   at root: unrecognized property 'events'
Serverless:   at 'functions.pollSpidersScrape.events[0]': unsupported function event
Serverless:  
Serverless: Learn more about configuration validation here: http://slss.io/configuration-validation
Serverless:  
Serverless: Generated requirements from /Users/Desktop/scrapy-serverless/scraper/requirements.txt in /Users/Desktop/scrapy-serverless/scraper/.serverless/requirements.txt...
Serverless: Using static cache of requirements found at /Users/Library/Caches/serverless-python-requirements/c31122786e70934d64a78fc1a8e1e713bf397712d1d00d20399c3fe5907a73b4_slspyc ...
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Injecting required Python packages to package...
Serverless: serverless-cloudformation-sub-variables: Converting AWS CloudFormation Sub variables...
Serverless:  - 'arn:aws:s3:::#{BucketName}/*' => 'arn:aws:s3:::${BucketName}/*'
Serverless:  - 'arn:aws:s3:::#{BucketName}' => 'arn:aws:s3:::${BucketName}'
Serverless:  - 'fargate-#{StackName}' => 'fargate-${StackName}'
Serverless:  - 'arn:aws:s3:::#{BucketName}/*' => 'arn:aws:s3:::${BucketName}/*'
Serverless:  - 'arn:aws:s3:::#{BucketName}/*' => 'arn:aws:s3:::${BucketName}/*'
Serverless:  - 'arn:aws:s3:::#{BucketName}' => 'arn:aws:s3:::${BucketName}'
Serverless:  - '#{AccountId}.dkr.ecr.#{Region}.amazonaws.com/#{Repo}' => '${AccountId}.dkr.ecr.${Region}.amazonaws.com/${Repo}'
Serverless:  - '#{AccountId}.dkr.ecr.#{Region}.amazonaws.com/#{Repo}' => '${AccountId}.dkr.ecr.${Region}.amazonaws.com/${Repo}'
 
 Serverless Error ----------------------------------------
 
  Unable to resolve '3' log group name out of: { RetentionInDays: 1 }

I assume that this error has to do with the configuration file, but I'm not sure what Unable to resolve '3' log group means. I'm quite sure the error originates from fargate-template.yml :

Resources:
  FargateECSCluster:
    Type: "AWS::ECS::Cluster"
    Properties:
      ClusterName: !Sub
        - "fargate-#{StackName}"
        - StackName: !Ref "AWS::StackName"

  FargateLogGroup:
    Type: "AWS::Logs::LogGroup"
    Properties:
      RetentionInDays: 1

  FargateTaskRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Sid: "ECS"
            Principal:
              Service:
                - ecs-tasks.amazonaws.com
            Action:
              - sts:AssumeRole
      Policies:
        - PolicyName: ScraperFeedBucketPolicy
          PolicyDocument:
            Statement:
              - Effect: "Allow"
                Action:
                  - "s3:PutObject"
                Resource: !Sub
                  - "arn:aws:s3:::#{BucketName}/*"
                  - BucketName: !Ref ScraperFeedBucket
        - PolicyName: HttpCacheBucketPolicy
          PolicyDocument:
            Statement:
              - Effect: "Allow"
                Action:
                  - "s3:GetObject"
                  - "s3:PutObject"
                  - "s3:ListBucket"
                Resource:
                  - !Sub
                    - "arn:aws:s3:::#{BucketName}/*"
                    - BucketName: !Ref HttpCacheBucket
                  - !Sub
                    - "arn:aws:s3:::#{BucketName}"
                    - BucketName: !Ref HttpCacheBucket

  FargateExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Sid: "ECS"
            Principal:
              Service:
                - ecs-tasks.amazonaws.com
            Action:
              - sts:AssumeRole

  FargateVPC:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: 10.10.10.0/24
      Tags:
        - Key: Name
          Value: !Ref "AWS::StackName"
  FargateSubnet:
    Type: "AWS::EC2::Subnet"
    Properties:
      CidrBlock: 10.10.10.0/24
      VpcId: !Ref FargateVPC
      Tags:
        - Key: Name
          Value: !Ref "AWS::StackName"
  FargateIGW:
    Type: "AWS::EC2::InternetGateway"
    Properties:
      Tags:
        - Key: Name
          Value: !Ref "AWS::StackName"
  FargateAttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref FargateVPC
      InternetGatewayId: !Ref FargateIGW
  FargateRouteTable:
    Type: "AWS::EC2::RouteTable"
    Properties:
      VpcId: !Ref FargateVPC
      Tags:
        - Key: Name
          Value: !Ref "AWS::StackName"
  FargateRoute:
    Type: "AWS::EC2::Route"
    DependsOn: FargateAttachGateway
    Properties:
      RouteTableId: !Ref FargateRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref FargateIGW
  FargateSubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref FargateRouteTable
      SubnetId: !Ref FargateSubnet
  FargateSG:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      GroupDescription: "Generated by Serverless"
      SecurityGroupIngress:
        - IpProtocol: -1
          CidrIp: 127.0.0.1/32
      Tags:
        - Key: Name
          Value: !Ref "AWS::StackName"
      VpcId: !Ref FargateVPC

  FargateECSRepo:
    Type: "AWS::ECR::Repository"
    Properties:
      LifecyclePolicy:
        LifecyclePolicyText: |
          {
            "rules": [
            {
              "rulePriority": 1,
              "description": "Only keep 8 images",
              "selection": {
                "tagStatus": "any",
                "countType": "imageCountMoreThan",
                "countNumber": 8
              },
              "action": { "type": "expire" }
            }]
          }

  FargateECSTaskDefinition:
    Type: "AWS::ECS::TaskDefinition"
    Properties:
      Cpu: 512
      Memory: 1GB
      NetworkMode: awsvpc
      RequiresCompatibilities:
        - "FARGATE"
      ExecutionRoleArn: !GetAtt FargateExecutionRole.Arn
      TaskRoleArn: !GetAtt FargateTaskRole.Arn
      ContainerDefinitions:
        - Name: scraper_container
          Image: !Sub
            - "#{AccountId}.dkr.ecr.#{Region}.amazonaws.com/#{Repo}"
            - AccountId: !Ref AWS::AccountId
              Region: !Ref AWS::Region
              Repo: !Ref FargateECSRepo
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref FargateLogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: !Ref AWS::StackName
Outputs:
  ECRRepo:
    Value: !Sub
      - "#{AccountId}.dkr.ecr.#{Region}.amazonaws.com/#{Repo}"
      - AccountId: !Ref AWS::AccountId
        Region: !Ref AWS::Region
        Repo: !Ref FargateECSRepo

How can I solve the above error? Again, I suspect this is a config issue since running docker build -t my_sls_scraper:latest. and then docker run my_sls_scraper runs the scraper without a problem on my local machine.

EDIT:

Removing FargateLogGroup doesn't help since it returns the following error: An error occurred: my-sls-scraper-dev - Template format error: Unresolved resource dependencies [FargateLogGroup] in the Resources block of the template. That is, FargateLogGroup must be included in the .yml .

I got it working by adding LogGroupName: 'someName' to FargateLogGroup :

  FargateLogGroup:
    Type: "AWS::Logs::LogGroup"
    Properties:
      LogGroupName: 'someName'
      RetentionInDays: 1

This is a documentation issue since the CloudFormation [documentation][2] mentions that none of the fields are required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM