Skip to content

Instantly share code, notes, and snippets.

@damienpontifex
Last active May 3, 2024 22:27
Show Gist options
  • Save damienpontifex/a773453a72685402743201ee75c873ef to your computer and use it in GitHub Desktop.
Save damienpontifex/a773453a72685402743201ee75c873ef to your computer and use it in GitHub Desktop.
Making the amplify CLI easier to use
Purposefully left blank for naming
#amplify
amplify/\#current-cloud-backend
amplify/.config/local-env-info.json
amplify/mock-data
amplify/backend/amplify-meta.json
amplify/backend/awscloudformation
amplify/team-provider-info.json
amplify/.config/local-aws-info.json
build/
dist/
node_modules/
aws-exports.js
awsconfiguration.json
amplifyconfiguration.json
amplify-build-config.json
amplify-gradle-config.json
amplifytools.xcconfig
#!/bin/bash
set -e # Stop on error
REPO_ROOT="$(dirname $0)/.."
ENV_NAME="${1}"
if [[ -z "${ENV_NAME}" ]]; then
>&2 echo "Provide environment name as first positional argument"
exit 1
else
echo "Deploying to ${ENV_NAME} environment"
fi
AMPLIFY_PROJECT_NAME=$(jq --raw-output '.projectName' "${REPO_ROOT}/amplify/.config/project-config.json")
AMPLIFY_PROJECT_NAME_LOWER=$(tr '[:upper:]' '[:lower:]' <<< ${AMPLIFY_PROJECT_NAME})
AMPLIFY_APP_ID=$(aws amplify list-apps --output json | jq -r 'first(.apps[] | select(.name=="$(AMPLIFY_PROJECT_NAME)")) | .appId')
if [[ -z "${AMPLIFY_APP_ID}" ]]; then
AMPLIFY_APP_ID=$(aws amplify create-app --name "${AMPLIFY_PROJECT_NAME}" --output json \
| jq --raw-output '.app.appId')
fi
AUTH_KEY=$(jq --raw-output '.auth | keys[0]' "${REPO_ROOT}/amplify/backend/backend-config.json")
AWSCONFIG="{\"configLevel\":\"project\",\"useProfile\":true,\"profileName\":\"default\"}"
AMPLIFY="{\"envName\":\"${ENV_NAME}\", \"projectName\": \"${AMPLIFY_PROJECT_NAME}\"}"
PROVIDERS="{\"awscloudformation\":${AWSCONFIG}}"
AUTHCONFIG="{\"${AUTH_KEY}\": {\"hostedUIProviderCreds\": \"[]\"}}"
CATEGORIES="{\"auth\":${AUTHCONFIG}}"
CF_STACK_ID=$(aws amplify get-backend-environment \
--app-id "${AMPLIFY_APP_ID}" \
--environment-name "${ENV_NAME}" --output json 2> /dev/null | jq --raw-output '.backendEnvironment.stackName')
if [[ -z "${CF_STACK_ID}" ]]; then
echo "No existing deployment for ${ENV_NAME} environment found, initialising new"
amplify init \
--appId "${AMPLIFY_APP_ID}" \
--amplify "${AMPLIFY}" \
--providers "${PROVIDERS}" \
--categories "${CATEGORIES}" \
--yes
else
echo "Existing deployment for ${ENV_NAME}"
CF_STACK_OUTPUT=$(aws cloudformation describe-stacks \
--output json \
--stack-name "${CF_STACK_ID}" \
--query "Stacks[0].Outputs" \
| jq "map({(.OutputKey): .OutputValue}) | add | { \"awscloudformation\": ., \"categories\": ${CATEGORIES} }")
echo "y" | amplify env import \
--name "${ENV_NAME}" \
--config "${CF_STACK_OUTPUT}" \
--awsInfo "${AWSCONFIG}" \
--categories "${CATEGORIES}" \
--yes
if [[ ! -f amplifyconfiguration.json ]]; then
amplify init \
--appId "${AMPLIFY_APP_ID}" \
--amplify "${AMPLIFY}" \
--providers "${PROVIDERS}" \
--categories "${CATEGORIES}" \
--yes
fi
fi
if [[ "$*" == *--push* ]]; then
echo "y" | amplify push --yes
amplify codegen statements
amplify codegen models --nodownload
amplify codegen types --nodownload
fi
#!/bin/bash
USERPOOLID=$(aws cognito-idp list-user-pools \
--max-results 10 \
--query "UserPools[?ends_with(Name, \`${1}\`) == \`true\`][Id]" \
--output text)
IDPEXIST=$(aws cognito-idp list-identity-providers \
--user-pool-id "${USERPOOLID}" \
--query 'Providers[?ProviderName==`AzureAD`]' \
--output text)
if [[ -z "${IDPEXIST}" ]]; then
aws cognito-idp create-identity-provider \
--user-pool-id "${USERPOOLID}" \
--cli-input-json file://cognito-idp.json
else
replace 's/"ProviderType": "OIDC",//' cognito-idp.json
aws cognito-idp update-identity-provider \
--user-pool-id "${USERPOOLID}" \
--cli-input-json file://cognito-idp.json
replace 's/"ProviderDetails":/"ProviderType": "OIDC", "ProviderDetails":/' cognito-idp.json
fi
APPCLIENTID=$(aws cognito-idp list-user-pool-clients \
--user-pool-id "${USERPOOLID}" \
--query 'UserPoolClients[?ends_with(ClientName, `app_client`) == `true`][ClientId]' \
--output text)
echo "AppClientId = ${APPCLIENTID}"
aws cognito-idp update-user-pool-client \
--user-pool-id "${USERPOOLID}" \
--client-id "${APPCLIENTID}" \
--cli-input-json file://cognito-idp-client.json
WORKSPACE ?= $(USER)
AMPLIFY_PROJECT_NAME := $(shell jq '.projectName' amplify/.config/project-config.json)
amplify: FORCE
@command -v amplify > /dev/null 2>&1 || npm install --global @aws-amplify/[email protected]
./amplify-push.sh $(WORKSPACE) --push
./cognito-idp.sh $(WORKSPACE)
set-amplify-app-id:
$(eval AMPLIFY_APP_ID := $(shell aws amplify list-apps --output json \
| jq 'first(.apps[] | select(.name=="$(AMPLIFY_PROJECT_NAME)")) | .appId'))
set-stack-id: set-amplify-app-id
$(eval CF_STACK_ID := $(shell aws amplify get-backend-environment \
--app-id $(AMPLIFY_APP_ID) \
--environment-name $(WORKSPACE) --output json 2> /dev/null | jq '.backendEnvironment.stackName')
set-amplify-s3-bucket-list:
$(eval AMPLIFY_S3_BUCKETS := $(shell aws s3 ls | awk '{print $$3}' | \
grep 'amplify-$(AMPLIFY_PROJECT_NAME_LOWER)-$(WORKSPACE)-'))
# Remove your backend environment from Amplify and delete deployed cloudformation stacks
destroy-amplify: set-stack-id set-amplify-app-id set-amplify-s3-bucket-list
@echo "Destroy amplify with app id $(AMPLIFY_APP_ID)"
@echo "Associated cloud formation stack: $(CF_STACK_ID)"
-aws cloudformation delete-stack --stack-name $(CF_STACK_ID)
-aws cloudformation wait stack-delete-complete --stack-name $(CF_STACK_ID)
$(foreach bucket_name,$(AMPLIFY_S3_BUCKETS), $(call delete-s3-bucket,$(bucket_name)))
-aws amplify delete-backend-environment \
--app-id $(AMPLIFY_APP_ID) \
--environment-name $(WORKSPACE)
jq 'del(.$(WORKSPACE))' amplify/team-provider-info.json > amplify/team-provider-info.json
clean:
rm -rf amplifyconfiguration.json \
awsconfiguration.json \
amplify/team-provider-info.json \
amplify/backend/amplify-meta.json \
amplify/backend/api/ironcloud/build \
amplify/.config/local-aws-info.json \
amplify/.config/local-env-info.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment