如何配置Golang可执行文件以在AWS中作为计划作业运行?

I have a static web site that will be running in AWS EC2. I also have a Golang executable that is intended to update some Javascript data files nightly. I understand that I need to set up a Worker Environment Tier to do this. I have two questions:

  1. Can such a job in a Worker Environment Tier write files in the Web Server Environment Tier?

  2. Since scheduled jobs are invoked via https POST, how do I configure my Golang executable to respond to POST requests when it isn't itself acting as the web server?

  1. What is the web server environment tier exactly? Is it an Elastic Beanstalk app? If it's just a static website you should be hosting it on S3, and if it is on S3 you can certainly write to that from your Golang executable.

  2. If you just want to run a nightly task, using Elastic Beanstalk worker environments seems like total overkill. Just run a small EC2 instance and schedule the task via cron. Alternatively, deploy it to Lambda via the Apex framework.

If you can get your static website working on S3 and your nightly jobs running on Lambda it will cost a small fraction of what Beanstalk and/or EC2 will.

As of January 2018, Lambda support Go, and running Lambda functions on a schedule is easy.

Take a look at this guide: Go Worker Functions with Lambda, S3, and CloudWatch Events.

The trick is to configure your Lambda function with a Schedule event, as well as permissions to access the S3 bucket. Here's an excerpt CloudFormation template:

Resources:
  WorkerPeriodicFunction:
    Properties:
      CodeUri: ./handlers/worker-periodic/main.zip
      Environment:
        Variables:
          BUCKET: !Ref Bucket
      Events:
        Request:
          Properties:
            Schedule: rate(1 day)
          Type: Schedule
      Handler: main
      Policies:
        - Statement:
            - Action:
                - s3:DeleteObject
              Effect: Allow
              Resource: !Sub "arn:aws:s3:::${Bucket}/*"
            - Action:
                - s3:ListBucket
              Effect: Allow
              Resource: !Sub "arn:aws:s3:::${Bucket}"
      Runtime: go1.x
      Timeout: 15
    Type: AWS::Serverless::Function

Check out my open-source gofaas Lambda and Go app to experiment with this technique.