I am trying to reproduce, step-by-step, the instructions on this video "Install AWS SAM CLI and create an S3 Bucket" from youtube, https://www.youtube.com/watch?v=EeLdSO6LHW0m, on how to create an S3 Bucket using SAM as Infrastructure as Code.
I have followed the video's instructions, step-by-step; however, when I run the aws-vault command to create the S3 bucket, I receive the following error message:
% aws-vault exec my-user --no-session -- sam deploy
File with same data already exists at resume-stack/445f94158914d32693176a0725a98472, skipping upload
Deploying with following values
===============================
Stack name : resume-stack
Region : us-east-1
Confirm changeset : False
Disable rollback : False
Deployment s3 bucket : aws-sam-cli-managed-default-samclisourcebucket-rzqvgcwwyja9
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {}
Signing Profiles : {}
Initiating deployment
=====================
Uploading to resume-stack/e89cc5a3136547707b1bcb1032c2d3b0.template 1350 / 1350 (100.00%)
Waiting for changeset to be created..
CloudFormation stack changeset
-----------------------------------------------------------------------------------------------------------------------------------------------------
Operation LogicalResourceId ResourceType Replacement
-----------------------------------------------------------------------------------------------------------------------------------------------------
+ Add MyWebsite AWS::S3::Bucket N/A
-----------------------------------------------------------------------------------------------------------------------------------------------------
Changeset created successfully. arn:aws:cloudformation:us-east-1:053806060854:changeSet/samcli-deploy1671330287/7674b5d9-9fb6-4187-8208-150664530af4
2022-12-17 21:25:00 - Waiting for stack create/update to complete
CloudFormation events from stack operations (refresh every 0.5 seconds)
-----------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus ResourceType LogicalResourceId ResourceStatusReason
-----------------------------------------------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS AWS::S3::Bucket MyWebsite -
CREATE_FAILED AWS::S3::Bucket MyWebsite my-resume-website already exists
UPDATE_ROLLBACK_IN_PROGRESS AWS::CloudFormation::Stack resume-stack The following resource(s) failed to
create: [MyWebsite].
UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN AWS::CloudFormation::Stack resume-stack -
_PROGRESS
DELETE_COMPLETE AWS::S3::Bucket MyWebsite -
UPDATE_ROLLBACK_COMPLETE AWS::CloudFormation::Stack resume-stack -
-----------------------------------------------------------------------------------------------------------------------------------------------------
Error: Failed to create/update the stack: resume-stack, Waiter StackUpdateComplete failed: Waiter encountered a terminal failure state: For expression "Stacks[].StackStatus" we matched expected path: "UPDATE_ROLLBACK_COMPLETE" at least once
My IAM user has the following permissions:
My template.yaml file:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
cloud-resume
Sample SAM Template for cloud-resume
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
MemorySize: 128
Resources:
MyWebsite:
Type: AWS::S3::Bucket
Properties:
BucketName: my-resume-website
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.9
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
I have deleted the stacks in CloudFormation and the all the S3 buckets created after running the sam deploy and re-run the sam deploy with using different names for the stack and the s3 bucket name in the template.yaml file, but I still get the same error message.
I also tried changing the resources block of code in template.yaml file as stated by this previous question: AWS Sam: Failed to create/update the stack Error
I changed the stanza in the template.yaml file from this:
Resources:
MyWebsite:
Type: AWS::S3::Bucket
Properties:
BucketName: my-resume-website
To the format recommended in the in the stackoverflow question mentioned above:
Parameters:
MyBucketName:
Type: String
Default: "resume-website"
Resources:
PatientCheckoutBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "prefix-${MyBucketName}-suffix"
This works, and it adds the S3 bucket successfully, but it names the bucket as "prefix-resume-website-suffix".
When I remove "prefix-" and "-suffix" from the BucketName, the sam deploy fails again.
How can I add the s3 bucket as "resume-website" without the "prefix-" and "-suffix"?
I would sincerely appreciate anyone's help with this.
Best,
Justin
Some resources on AWS need to be globally unique (eg s3 bucket names) ie not used on AWS anywhere else, and some need to be unique just within your AWS account and a specific region (eg lambda & SQS names)
I suspect you're running into issues with bucket names that have been previously taken, what I would suggest is a simple naming convention to avoid naming conflicts on all resources (especially for production resources within Companies with multiple developers to avoid naming collisions, and to make it easier to see which resources belong to which stacks at a glance) is to prefix the resource name with your stack-name, eg:
Resources:
# s3 Buckets
MyResumeWebsite:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${AWS::StackName}-my-resume-website
# lambdas
SomeLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-SomeLambda
# SQSs
SomeSQS:
Type: AWS::SQS::Queue
Properties:
QueueName: !Sub ${AWS::StackName}-SomeSQS