In this section, you will deploy the backend for your dynamic e-commerce website using SAM CLI and CloudFormation. We will use the provided template.yaml
file in your project to automatically create resources such as API Gateway, Lambda, and DynamoDB.
Before using SAM CLI for deployment, you need to configure AWS CLI with your AWS account credentials. This allows SAM CLI to use the access rights granted to your IAM User.
Run the aws configure
command:
Open your terminal and enter the following command to configure AWS CLI:
aws configure
Enter your configuration information:
AWS Access Key ID: Enter the Access Key ID generated when creating your IAM User.
AWS Secret Access Key: Enter the Secret Access Key associated with your Access Key ID.
Default region name: Enter ap-southeast-1
(Singapore).
Default output format: You can enter json
or leave it as the default (None
).
Once finished, AWS CLI will save the configuration in the ~/.aws/credentials
file (Linux/macOS) or C:\Users\<username>\.aws\credentials
(Windows).
Check the template.yaml
file:
template.yaml
file, check your project directory. This file defines resources like API Gateway, Lambda, and DynamoDB.Build the project:
After ensuring that template.yaml
is in your project,
Navigate to your backend project folder and use SAM CLI to build:
sam build
If you see Build Succeeded, you have successfully installed and configured SAM CLI.
Validate the Template:
Check your template.yaml
configuration to ensure there are no syntax or configuration errors:
Run sam validate to check your template:
sam validate
If you see the message below, your template.yaml is valid.
Deploy resources to AWS:
After building and validating, deploy resources to AWS using:
sam deploy --guided
When using SAM CLI to deploy, you’ll be prompted for configuration options. Suggested values:
sam-app
ap-southeast-1
y
y
n
(do not disable rollback)y
y
y
Verify Deployment:
After successful deployment, SAM CLI will provide information about the resources created. You can check API Gateway, Lambda function, S3, CloudFormation, and DynamoDB in the AWS Management Console to confirm that everything has been set up correctly.
Your backend has been successfully deployed! Resources like API Gateway, Lambda function, and DynamoDB have been automatically created. You can check them in the AWS Console.