Chapter 9: Building the Frontend with S3

  1. Implement a Lambda function that takes the movie category as input and returns a list of movies that corresponds to that category.
    Answer:
func filter(category string)(events.APIGatewayProxyResponse, error) {
...

filter: = expression.Name("category").Equal(expression.Value(category))
projection: = expression.NamesList(expression.Name("id"), expression.Name("name"), expression.Name("description"))
expr, err: = expression.NewBuilder().WithFilter(filter).WithProjection(projection).Build()
if err != nil {
return events.APIGatewayProxyResponse {
StatusCode: http.StatusInternalServerError,
Body: "Error while building DynamoDB expression",
}, nil
}

svc: = dynamodb.New(cfg)
req: = svc.ScanRequest( & dynamodb.ScanInput {
TableName: aws.String(os.Getenv("TABLE_NAME")),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
FilterExpression: expr.Filter(),
ProjectionExpression: expr.Projection(),
})

...
}
  1. Implement a Lambda function that takes a movie's title as input and returns all movies that have the keyword in their title.
    Answer:
func filter(keyword string) (events.APIGatewayProxyResponse, error) {
...

filter := expression.Name("name").Contains(keyword)
projection := expression.NamesList(expression.Name("id"), expression.Name("name"), expression.Name("description"))
expr, err := expression.NewBuilder().WithFilter(filter).WithProjection(projection).Build()
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusInternalServerError,
Body: "Error while building DynamoDB expression",
}, nil
}

svc := dynamodb.New(cfg)
req := svc.ScanRequest(&dynamodb.ScanInput{
TableName: aws.String(os.Getenv("TABLE_NAME")),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
FilterExpression: expr.Filter(),
ProjectionExpression: expr.Projection(),
})
...
}
  1. Implement a delete button on the web application to delete a movie by calling the DeleteMovie Lambda function from API Gateway.

Answer: Update the MoviesAPI service to include the following function:

delete(id: string){
return this.http
.delete(`${environment.api}/${id}`, {headers: this.getHeaders()})
.map(res => {
return res
})
}
  1. Implement an edit button on the web application to allow the user to update movie attributes.
    Answer:
update(movie: Movie){
return this.http
.put(environment.api, JSON.stringify(movie), {headers: this.getHeaders()})
.map(res => {
return res
})
}
  1. Implement a CI/CD workflow with either CircleCI, Jenkins, or CodePipeline to automate the generation and deployment of the API Gateway documentation.
    Answer:
def bucket = 'movies-api-documentation'
def api_id = ''

node('slaves'){
stage('Generate'){
if (env.BRANCH_NAME == 'master') {
sh "aws apigateway get-export --rest-api-id ${api_id} \
--stage-name production \
--export-type swagger swagger.json"
}
else if (env.BRANCH_NAME == 'preprod') {
sh "aws apigateway get-export --rest-api-id ${api_id} \
--stage-name staging \
--export-type swagger swagger.json"
} else {
sh "aws apigateway get-export --rest-api-id ${api_id} \
--stage-name sandbox \
--export-type swagger swagger.json"
}
}

stage('Publish'){
sh "aws s3 cp swagger.json s3://${bucket}"
}
}