Search code examples
amazon-s3serverless-application-model

How to specify S3 bucket region SAM template


I'm learning AWS Serverless Application Model. I'm trying the following model:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
  MyLambdaFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      Runtime: nodejs8.10
      Handler: index.handler
      CodeUri: 
        Bucket: artifacts-for-lambda
        Key: my-lambda-package.zip
      Events:
        MySchedule:
          Type: Schedule
          Properties:
            Schedule: rate(1 minute)
        MyS3Upload:
          Type: S3
          Properties:
            Bucket: !Ref MyS3Bucket
            Events: s3:ObjectCreated:*
  MyS3Bucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: upload-something-here

This is how I'm running it:

aws cloudformation deploy 
--capabilities CAPABILITY_NAMED_IAM 
--template-file sam-template.yaml 
--stack-name my-serverless-app

This is the error I'm receiving:

Error occurred while GetObject. S3 Error Code: PermanentRedirect. S3 Error Message: The bucket is in this region: us-east-1. Please use this region to retry the request (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException

us-east-2 is my default region per my AWS config file.

If us-east-2 is my default region why am I getting this error message saying The bucket is in this region: us-east-1? How do I specify a region for my S3 bucket in my serverless script?


Solution

  • Tom, I used SAM in one of the projects I was working on. You can use it like this:

    sam package --template-file template.yml \
                  --output-template-file packaged.yml \
                  --s3-bucket developing-sam-applications 
                  --region YOUR_REGION
    

    Moreover, you can deploy using this command with the region specified:

    sam deploy --template-file packaged.yml \
                 --stack-name developing-sam-applications \
                 --capabilities CAPABILITY_IAM
                 --region YOUR_REGION
    

    Note: Make sure, you have bucket and function in the same region. If you want to deploy on different region, you'll need a bucket in that region.