In this step, we will create an S3 bucket with Static web hosting enabled and publicly accessible based on SAM:
Open template.yaml file in fcj-book-shop folder that we created in part 1.
Copy the following script into that file:
FcjBookShop:
Type: AWS::S3::Bucket
Properties:
AccessControl: PublicRead
BucketName: fcj-book-shop
WebsiteConfiguration:
IndexDocument: index.html
FcjBookShopPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref FcjBookShop
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- 's3:GetObject'
Effect: Allow
Principal: '*'
Resource: !Join
- ''
- - 'arn:aws:s3:::'
- !Ref FcjBookShop
The above script defines an S3 bucket is fcj-book-shop with FcjBookShopPolicy policy - allow public access
Run the below command:
To build at the directory of the SAM project: fcj-book-shop
sam build
To check the validation of the SAM template
sam validate
To deploy SAM
sam deploy --guided
Enter stack name: fcj-book-shop
Enter the deployemnt region, such as: us-east-1
- should be the same as the default region
Then enter other information as shown below
Wait a while to create the CloudFormation stack changeset
Enter “y” when Deploy this changeset? display
Open Amazon S3 console
Click Properties tab. Then scroll down, check state of Static website hosting
Click Permissions tab
Open CloudFormation console. Two stacks have been created
Click Resource tab, see the resources that CloudFormation has initialized
Download fcj-serverless-frontend code to your device
git clone https://github.com/AWS-First-Cloud-Journey/FCJ-Serverless-Workshop.git
cd FCJ-Serverless-Workshop
yarn build
We have finished building the front-end. Next, execute the following command to upload the build folder to S3
aws s3 cp build s3://fcj-book-shop --recursive
Result after uploading: