Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a demo script to exemplify the blogpost #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions api_template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
AWSTemplateFormatVersion: 2010-09-09
Resources:
TestApi:
Type: 'AWS::ApiGateway::RestApi'
Properties:
Name: viesure-blog-gateway
ApiDeployment:
Type: 'AWS::ApiGateway::Deployment'
DependsOn: ApiRequest
Properties:
RestApiId:
Ref: TestApi
StageName: test
ApiResource:
Type: 'AWS::ApiGateway::Resource'
Properties:
RestApiId:
Ref: TestApi
ParentId: !GetAtt TestApi.RootResourceId
PathPart: helloworld
ApiRequest:
Type: 'AWS::ApiGateway::Method'
Properties:
AuthorizationType: NONE
HttpMethod: GET
Integration:
Type: MOCK
RequestTemplates:
application/json: "{\n \"statusCode\": 200\n}"
IntegrationResponses:
- StatusCode: 200
MethodResponses:
- StatusCode: 200
ResourceId:
Ref: ApiResource
RestApiId:
Ref: TestApi
55 changes: 55 additions & 0 deletions script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

###########################
# Account ID and Region #
###########################

# get current AWS Account ID
ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text)
# get current AWS REGION to use in URLs
REGION=$(aws configure get region)

echo "Your account ID is ${ACCOUNT_ID}"
echo "Your region is ${REGION}"
echo ""

###########################
# REST Gateway Endpoint #
###########################
echo "Creating Gateway for test..."
STACKNAME=viesure-blog-gateway
aws cloudformation deploy --template-file api_template.yaml --stack-name ${STACKNAME}
# Wait for the Gateway to be available.
# This may take a while since the wait command only checks every 30s
aws cloudformation wait stack-create-complete --stack-name ${STACKNAME}

REST_API_ID=$(aws apigateway get-rest-apis --query 'items[?name==`viesure-blog-gateway`].id' --output text)
STAGE=test
API_URL=https://${REST_API_ID}.execute-api.${REGION}.amazonaws.com/${STAGE}
echo "Your API URL is ${API_URL}"
ENDPOINT=${API_URL}/helloworld
echo "calling the endpoint ${ENDPOINT}"
if curl --fail ${ENDPOINT} ; then
echo "Successfully called API!"
else
echo "Failed to call API"
fi

echo "Deleting gateway..."
aws cloudformation delete-stack --stack-name ${STACKNAME}
echo ""

####################################
# Elastic Container Registry URL #
####################################
echo "Creating ECR repository for test..."
REPOSITORY=viesure-blog-repository
# we could use the output of create-repository too... but that'd defeat the purpose of the demonstration ;)
aws ecr create-repository --repository-name ${REPOSITORY} > /dev/null
ECR_URL_CONSTRUCTED=${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/${REPOSITORY}
ECR_URL_QUERIED=$(aws ecr describe-repositories --repository-names ${REPOSITORY} --query 'repositories[*].repositoryUri' --output text)
echo "Constructed ECR URL: ${ECR_URL_CONSTRUCTED}"
echo "Constructed ECR URL: ${ECR_URL_QUERIED}"

echo "Deleting repository..."
aws ecr delete-repository --repository-name ${REPOSITORY} > /dev/null