3

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?

Tom Schreck
  • 5,177
  • 12
  • 68
  • 122

1 Answers1

2

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.

Hassan Murtaza
  • 963
  • 6
  • 15
  • I'm a bit confused. You are packaging template.yml to produce packaged.yml then deploying packaged.yml. I followed your process above and packaged.yml looks exactly like template.yml. Why is the package step necessary? Can I simply deploy template.yml? – Tom Schreck Sep 16 '19 at 15:01
  • I added --region us-east-2 to both sam package and sam deploy. I'm still getting the 'The bucket is in this region: us-east-1' error message. – Tom Schreck Sep 16 '19 at 15:02
  • So @TomSchreck First command use template.yml (which is SAM template) and output the cloudformation template (packagaed.yml). In the next command, sam deploy I'm using template-file packaged.yml (Cloudformation template) and deploying it. Now, do you have your bucket already created with the name? If yes, which region already bucket is in? Just to let you know, S3 buckets are created in a specific region in AWS. – Hassan Murtaza Sep 16 '19 at 15:06
  • No, I do not have the bucket created yet. I'm under the impression the yml template will also create the S3 bucket. Do I need to manually create the S3 bucket first? – Tom Schreck Sep 16 '19 at 15:08
  • You're right about that. A bucket should be created with this template. 1. Can you try changing the bucket name? 2. Can you try deploying to us-east-1 for now? – Hassan Murtaza Sep 16 '19 at 15:16
  • I've changed my default region to us-east-1 and I get past this issue. I'm trying to learn and understand how this all works. Is it even possible to use SAM/CloudFormation deploy a S3 bucket to a region other than us-east-1? I understand the function and the bucket need to be in the same region. That makes sense to me. However, I'm not able to create a S3 bucket in a region other than us-east-1. – Tom Schreck Sep 16 '19 at 15:25
  • @TomSchreck Did you try changing the bucket name and keeping the region us-east-2? – Hassan Murtaza Sep 16 '19 at 15:30
  • 1
    I got it to work for us-east-2. I was confused about how package and deploy worked. My confusion was around how package creates a new yaml file and the deploy should use the new yaml file generated from package command. I was looking at this incorrectly. I was assuming I just needed to deploy the sam-template.yaml without first calling package. Thank you for your help. – Tom Schreck Sep 16 '19 at 16:48
  • Glad I was able to help! @TomSchreck – Hassan Murtaza Sep 16 '19 at 17:13