|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +###################################################################### |
| 4 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # |
| 5 | +# SPDX-License-Identifier: MIT-0 # |
| 6 | +###################################################################### |
| 7 | + |
| 8 | +# Source helper functions |
| 9 | +source .fun |
| 10 | + |
| 11 | +# Proxy settings [optional] - set if your network requires a proxy to connect to the Internet |
| 12 | +export http_proxy= |
| 13 | +export https_proxy= |
| 14 | +export no_proxy=localhost |
| 15 | + |
| 16 | +# Project settings |
| 17 | +## CMD_EDIT - text editor command used when editing config file. Default CMD_EDIT='vim -c ":syntax on"' |
| 18 | +export CMD_EDIT='vim -c ":syntax on"' |
| 19 | +## Target orchestrator TO=docker|compose|ecs|swarm|kubernetes|eks|lambda|batchlocal|batch |
| 20 | +## docker - choose when running locally or on ec2 |
| 21 | +## compose - choose when running locally, on ec2 |
| 22 | +## ecs - choose when running on ecs with or without Fargate |
| 23 | +## swarm - choose when running on a docker swarm |
| 24 | +## kubernetes - choose when running on a local, remote Kubernetes cluster or EKS, with or without Fargate |
| 25 | +## lambdalocal - choose when developing and testing an AWS Lambda container locally |
| 26 | +## lambda - choose when running containerized function on AWS Lambda |
| 27 | +## batchlocal - choose when running containerized jobs locally |
| 28 | +## batch - choose when running containerized jobs on AWS Batch |
| 29 | +export TO=docker |
| 30 | +## DOCKERFILE - dockerfile to use for this project DOCKERFILE=./Dockerfile|./Dockerfile.lambda. If TO=lambda, then set DOCKERFILE=./Dockerfile.lambda |
| 31 | +export DOCKERFILE_EXT="" |
| 32 | +if [[ "$TO" == "lambda" || "$TO" == "lambdalocal" ]]; then |
| 33 | + export DOCKERFILE_EXT="-lambda" |
| 34 | +fi |
| 35 | +export DOCKERFILE=./Dockerfile${DOCKERFILE_EXT} |
| 36 | +## VERBOSE - show verbose output, VERBOSE=true|false |
| 37 | +export VERBOSE=true |
| 38 | +## DEBUG - turn on script debugging, DEBUG=false|true |
| 39 | +export DEBUG=false |
| 40 | +## DRY_RUN - do not execute commands, DRY_RUN=false|true |
| 41 | +export DRY_RUN=false |
| 42 | + |
| 43 | +# Docker image settings |
| 44 | +## BASE_IMAGE - your image will be built FROM this BASE_IMAGE as a starting point |
| 45 | +export BASE_IMAGE_PATH=python:3.9 |
| 46 | +## MAINTAINER - the user who owns and maintains this image |
| 47 | +export MAINTAINER="$(whoami)" |
| 48 | +## DESCRIPTION - short description of what your image does |
| 49 | +export DESCRIPTION="Simple Parrot API - providing a healthcheck and repeating requested text" |
| 50 | +## BUILD - a user-friendly build identifier to distinguish between two images that have the same tag |
| 51 | +export BUILD=$(date +%Y%m%d%H%M) |
| 52 | +## REGISTRY: [optional] - Docker registry path including trailing "/". Example: registry.company.com/demo/ |
| 53 | +export REGISTRY= |
| 54 | +if [ -n "${REGISTRY}" ]; then |
| 55 | + if [ "${REGISTRY: -1}" != "/" ]; then |
| 56 | + export REGISTRY="${REGISTRY}/" |
| 57 | + fi |
| 58 | +fi |
| 59 | +## REGISTRY_TYPE - type of container registry, used by login.sh. REGISTRY_TYPE=ecr |
| 60 | +export REGISTRY_TYPE=ecr |
| 61 | +## IMAGE_NAME - name of the Docker image for this project. Example: parrot |
| 62 | +export IMAGE_NAME=parrot |
| 63 | +## IMAGE - derived Docker image name, do not edit. |
| 64 | +export IMAGE=${IMAGE_NAME}${DOCKERFILE_EXT} |
| 65 | +## VERSION: [optional] - Version tag for this Docker image. Example: v20180302 |
| 66 | +#export VERSION=v$(date +%Y%m%d) |
| 67 | +export VERSION=latest |
| 68 | +export TAG=$(if [ -z "${VERSION}" ]; then echo ""; else echo ":${VERSION}"; fi) |
| 69 | +## BUILD_OPTS: [optional] - arguments for the docker image build command |
| 70 | +export BUILD_OPTS="--file ${DOCKERFILE} --build-arg BUILD=${BUILD} --build-arg BASE_IMAGE_PATH=${BASE_IMAGE_PATH} --build-arg MAINTAINER=\"${MAINTAINER}\" --build-arg DESCRIPTION=\"${DESCRIPTION}\" --build-arg http_proxy=${http_proxy} --build-arg https_proxy=${https_proxy} --build-arg no_proxy=${no_proxy}" |
| 71 | + |
| 72 | +# Target Orchestrator (TO) Settings |
| 73 | +## common |
| 74 | +### CONTAINER_NAME: [optional] - Name of the Docker container including the --name switch. Example --name myapp |
| 75 | +export CONTAINER=${IMAGE}-${TO} |
| 76 | +export CONTAINER_NAME="--name ${CONTAINER}" |
| 77 | +### PORT_INTERNAL - port on which the application runs inside the container, if more than one port is needed, define additioal variables here |
| 78 | +export PORT_INTERNAL=80 |
| 79 | +### PORT_EXTERNAL - port to expose outside the container, if more than one port is needed, define additional variables here |
| 80 | +### firstAvailable is a function defined in .fun. For local orchestrators, it returns the first available port, starting from the one requested in the argument |
| 81 | +export PORT_EXTERNAL=$(firstAvailable 80) |
| 82 | +### VOLUME_PATH_INTERNAL - mount path of volume inside the container |
| 83 | +export VOLUME_PATH_INTERNAL=/wd |
| 84 | +## VOLUME_PATH_EXTERNAL - external path to mount |
| 85 | +export VOLUME_PATH_EXTERNAL=$(pwd) |
| 86 | +### ENVSUBST - envsubst cli. ENVSUBST=envsubst | "env > /tmp/myanv && docker run --rm -it --env-file /tmp/myenv -v $(pwd):$(pwd) iankoulski/envsubst sh -c envsubst" |
| 87 | +export ENVSUBST=envsubst |
| 88 | +### REGION - the region where container will be deployed. Example: REGION=us-west-2 |
| 89 | +export REGION=us-west-2 |
| 90 | + |
| 91 | +## docker |
| 92 | +### Port map [optional] - Mapping of external to internal ports including the -p switch. Example -p 80:8080 |
| 93 | +export PORT_MAP="-p ${PORT_EXTERNAL}:${PORT_INTERNAL}" |
| 94 | +### Volume map [optional] - Mapping of external to internal paths including the -v switch. Example $(pwd):/wd |
| 95 | +export VOL_MAP="-v ${VOLUME_PATH_EXTERNAL}:${VOLUME_PATH_INTERNAL}" |
| 96 | +### Network [optional] - Network name including the --net switch. Example --net mynet |
| 97 | +export NETWORK= |
| 98 | +### RUN_OPTS [optional] - additional options to specify with the run comman. Example -e POSTGRES_DB=dbname |
| 99 | +export RUN_OPTS="-e PORT_INTERNAL=$PORT_INTERNAL -e ITERATION_LIMIT=3 -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e no_proxy=$no_proxy" |
| 100 | + |
| 101 | +## compose |
| 102 | +### DOCKER_COMPOSE - docker-compose cli, DOCKER_COMPOSE=docker-compose | "docker compose", |
| 103 | +### please use docker-compose with traditional docker-compose and "docker compose" with Docker Desktop |
| 104 | +export DOCKER_COMPOSE="docker compose" |
| 105 | +### COMPOSE_CONTEXT_TYPE - docker compose context type, COMPOSE_CONTEXT_TYPE=moby|ecs, |
| 106 | +### please use moby when running against a docker daemon, or ecs when depoying to an AWS ECS context. |
| 107 | +COMPOSE_CONTEXT_TYPE=moby |
| 108 | +### COMPOSE_PROJECT_NAME - prefix for project containers, example COMPOSE_PROJECT_NAME=compose |
| 109 | +export COMPOSE_PROJECT_NAME=compose |
| 110 | +### COMPOSE_TEMPLATE_PATH - folder containing compose file templates |
| 111 | +export COMPOSE_TEMPLATE_PATH=./to/compose/template |
| 112 | +### COMPOSE_APP_PATH - folder containig generated compose files |
| 113 | +export COMPOSE_APP_PATH=./to/compose/app |
| 114 | +### COMPOSE_TEST_PATH - folder containing deployment test scripts |
| 115 | +export COMPOSE_TEST_PATH=./to/compose/test |
| 116 | +### COMPOSE_FILE - file path of the docker-compose file for the current app |
| 117 | +export COMPOSE_FILE=${COMPOSE_APP_PATH}/docker-compose.yaml |
| 118 | + |
| 119 | +## ecs |
| 120 | +### ECS_CLI - ecs-cli command, ECS_CLI=ecs-cli |
| 121 | +export ECS_CLI=ecs-cli |
| 122 | +### ECS_CLUSTER_NAME - name of the ECS cluster to use, if the cluster does not exist, it will be created, ECS_CLUSTER_NAME=default |
| 123 | +export ECS_CLUSTER=default |
| 124 | +### ECS_MANAGE_CLUSTER - when true create and remove ECS cluster upon run or stop of container, when false assume cluster exists, ECS_MANAGE_CLUSTER=false|true |
| 125 | +ECS_MANAGE_CLUSTER=true |
| 126 | +### ECS_LAUNCH_TYPE - default launch type for ecs tasks, determines if tasks run on EC2 or Fargate, ECS_LAUNCH_TYPE=EC2|FARGATE |
| 127 | +export ECS_LAUNCH_TYPE=EC2 |
| 128 | +### ECS_COMPOSE_FILE - file path to the docker-compose file for the current ECS app, generated from template |
| 129 | +export ECS_COMPOSE_FILE=${COMPOSE_APP_PATH}/ecs-compose.yaml |
| 130 | +### ECS_PARAMS_FILE - file with ECS-specific task definition parameters |
| 131 | +export ECS_PARAMS_FILE=${COMPOSE_APP_PATH}/ecs-params-${ECS_LAUNCH_TYPE}.yaml |
| 132 | +### ECS_TRUST_FILE - file with IAM principal information for ecsTaskExecitonRole and ecsTaskRole |
| 133 | +export ECS_TRUST_FILE=${COMPOSE_APP_PATH}/ecs-trust.json |
| 134 | +### ECS_EXEC_POLICY_FILE - file with definition of IAM policy ecsExecPolicy for ecsTaskRole |
| 135 | +export ECS_EXEC_POLICY_FILE=${COMPOSE_APP_PATH}/ecs-exec-policy.json |
| 136 | +### ECS_SG_CIDR - security group CIDR from which to allow connections to the containers, ECS_SG_CIDR=0.0.0.0/0 |
| 137 | +export ECS_SG_CIDR="0.0.0.0/0" |
| 138 | +### ECS_MEM_LIMIT - memory limit for container, ECS_MEM_LIMIT="0.5GB" |
| 139 | +export ECS_MEM_LIMIT="0.5GB" |
| 140 | +### ECS_CPU_LIMIT - cpu limit for container, 1 vCPU = 1024, ECS_CPU_LIMIT=256 |
| 141 | +export ECS_CPU_LIMIT=256 |
| 142 | +### ECS_ASSIGN_PUBLIC_IP - auto-assign public ip to container, ECS_ASSIGN_PUBLIC_IP=ENABLED|DISABLED |
| 143 | +export ECS_ASSIGN_PUBLIC_IP=ENABLED |
| 144 | + |
| 145 | +## swarm |
| 146 | +### SWARM_STACK_NAME - name of the application stack to deploy, similar to COMPOSE_PROJECT_NAME |
| 147 | +export SWARM_STACK_NAME=mystack |
| 148 | +### SWARM_SERVICE_NAME - a stack may contain many services. The action scripts are executed agains the service name configured here. |
| 149 | +export SWARM_SERVICE_NAME=${CONTAINER} |
| 150 | + |
| 151 | +## kubernetes |
| 152 | +### KUBECTL - kubectl cli, KUBECTL=kubectl |
| 153 | +export KUBECTL=kubectl |
| 154 | +### KUBETAIL - kubetail cli, KUBETAIL=kubetail |
| 155 | +export KUBETAIL=kubetail |
| 156 | +### KUBERNETES_TEMPLATE_PATH - folder containing Kubernetes manifest templates |
| 157 | +export KUBERNETES_TEMPLATE_PATH=./to/kubernetes/template |
| 158 | +### KUBERNETES_APP_PATH - folder containing Kubernetes manifests |
| 159 | +export KUBERNETES_APP_PATH=./to/kubernetes/app |
| 160 | +### APP_NAME - name of the appliation to deploy |
| 161 | +export APP_NAME=${CONTAINER} |
| 162 | +### APP_DNS_NAME - fully qualified domain name for the application |
| 163 | +export APP_DNS_NAME=${CONTAINER}.domain.ext |
| 164 | +### NAMESPACE - Kubernetes namespace where the app will run |
| 165 | +export NAMESPACE=${APP_NAME} |
| 166 | +### INSTANCE_TYPE - optional instance type on which application pods should run. To use, uncomment deployment template nodeSelector section. |
| 167 | +export INSTANCE_TYPE=c5.2xlarge |
| 168 | + |
| 169 | +## lambda |
| 170 | +### see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-images.html |
| 171 | +### LAMBDA_FUNCTION_NAME - name of the function to create, LAMBDA_FUNCTION_NAME=${CONTAINER} |
| 172 | +export LAMBDA_FUNCTION_NAME=${CONTAINER} |
| 173 | +### LAMBDA_TEMPLATE_PATH - path to lambda-related templates LAMBDA_TEMPLATE_PATH=./to/lambda/template |
| 174 | +export LAMBDA_TEMPLATE_PATH=./to/lambda/template |
| 175 | +### LAMBDA_APP_PATH - path to lambda related files generated from templates LAMBDA_APP_PATH=./to/lambda/app |
| 176 | +export LAMBDA_APP_PATH=./to/lambda/app |
| 177 | +### LAMBDA_TEST_PATH - path to directory containing test scripts for deployed lambda function |
| 178 | +export LAMBDA_TEST_PATH=./to/lambda/test |
| 179 | +### LAMBDA_TRUST_FILE - file with IAM principal information for lambdaFunctionRole |
| 180 | +export LAMBDA_TRUST_FILE=${LAMBDA_APP_PATH}/lambda-trust.json |
| 181 | +## LAMBDA_POLICY_FILE - file with definition of IAM policy lambdaFunctionPolicy for lambdaFunctionRole |
| 182 | +export LAMBDA_POLICY_FILE=${LAMBDA_APP_PATH}/lambda-policy.json |
| 183 | + |
| 184 | +## batch |
| 185 | +### BATCH_NAME - app name for the current batch workload |
| 186 | +export BATCH_NAME=${CONTAINER} |
| 187 | +### BATCH_COMPUTE_ENVIRONMENT_NAME - name of the compute environment to submit jobs to, BATCH_COMPUTE_ENVIRONMENT_NAME=${BATCH_NAME}-compute |
| 188 | +export BATCH_COMPUTE_ENVIRONMENT_NAME=${BATCH_NAME}-compute |
| 189 | +### BATCH_JOB_QUEUE_NAME - name of the job queue for the current batch, BATCH_JOB_QUEUE_NAME=${BATCH_NAME}-job-queue-yyyymmdd |
| 190 | +#export BATCH_JOB_QUEUE_NAME=${BATCH_NAME}-job-queue-$(date +%Y%m%d) |
| 191 | +export BATCH_JOB_QUEUE_NAME=${BATCH_NAME}-job-queue |
| 192 | +### BATCH_JOB_DEFINITION_NAME - name of the batch job definition, BATCH_JOB_DEFINITION_NAME=${BATCH_NAME}-job-definition |
| 193 | +export BATCH_JOB_DEFINITION_NAME=${BATCH_NAME}-job-definition |
| 194 | +### BATCH_JOB_NAME - name of the batch job, BATCH_JOB_NAME=${BATCH_NAME}-job |
| 195 | +export BATCH_JOB_NAME=${BATCH_NAME}-job |
| 196 | +### BATCH_COMMAND_DEFAULT - the default command to execute in the job container, BATCH_COMMAND_DEFAULT=["/job/startup.sh"] |
| 197 | +export BATCH_COMMAND_DEFAULT="/job/startup.sh" |
| 198 | +### BATCH_JOB_ENV_VARS - environment variables to set in the job container, BATCH_JOB_ENV_VARS="[{name=NAME1,value=VALUE1},{name=NAME2,value=VALUE2}]" |
| 199 | +export BATCH_JOB_ENV_VARS="[{name=ITERATION_LIMIT,value=6}]" |
| 200 | +### BATCH_MANAGE_COMPUTE_ENVIRONMENT - if true create and delete compute environment upon run or stop, if false assume compute environment exists, BATCH_MANAGE_COMPUTE_ENVIRONMENT=true|false |
| 201 | +export BATCH_MANAGE_COMPUTE_ENVIRONMENT=true |
| 202 | +### BATCH_JOB_VCPUS - number of vCPUs to assign to the batch job, BATCH_JOB_VCPUS=1 |
| 203 | +### The combination of VCPUS and MEMORY has to be supported according to https://docs.aws.amazon.com/cli/latest/reference/batch/register-job-definition.html |
| 204 | +### Examples: VCPUS=0.5, MEMORY=1024,2048,3072,4096; VCPUS=1, MEMORY=2048,3072,4096,5120,6144,7168,8192 |
| 205 | +export BATCH_JOB_VCPUS=1 |
| 206 | +## BATCH_JOB_MEMORY - memory limit in MiB for the container, BATCH_JOB_MEMORY=2048 |
| 207 | +export BATCH_JOB_MEMORY=2048 |
| 208 | +### BATCH_COMPUTE_ENVIRONMENT_TYPE - type of compute to use, BATCH_COMPUTE_ENVIRONMENT_TYPE=EC2|FARGATE |
| 209 | +export BATCH_COMPUTE_ENVIRONMENT_TYPE=EC2 |
| 210 | +### BATCH_COMPUTE_RESOURCES - compute environment configuration depending on type. |
| 211 | +### It is assumed that a VPC already exists. Specify subnets where you would like batch jobs to run. |
| 212 | +### Specify at least one valid security group. The vpc default security group is acceptable to use here. |
| 213 | +### The full list of possible settings is below |
| 214 | +### export BATCH_COMPUTE_RESOURCES="type=string,allocationStrategy=string,minvCpus=integer,maxvCpus=integer,desiredvCpus=integer,instanceTypes=string,string,imageId=string,subnets=string,string,securityGroupIds=string,string,ec2KeyPair=string,instanceRole=string,tags={KeyName1=string,KeyName2=string},placementGroup=string,bidPercentage=integer,spotIamFleetRole=string,launchTemplate={launchTemplateId=string,launchTemplateName=string,version=string},ec2Configuration=[{imageType=string,imageIdOverride=string},{imageType=string,imageIdOverride=string}]" |
| 215 | +if [ "${BATCH_COMPUTE_ENVIRONMENT_TYPE}" == "EC2" ]; then |
| 216 | + export BATCH_COMPUTE_RESOURCES="type=EC2,minvCpus=0,maxvCpus=256,instanceTypes=optimal,instanceRole=ecsInstanceRole,subnets=subnet-0e11612f928c54936,subnet-0003aacf876f0fac1,securityGroupIds=sg-097730877ae293efd" |
| 217 | +else |
| 218 | + export BATCH_COMPUTE_RESOURCES="type=FARGATE,maxvCpus=256,subnets=subnet-0e11612f928c54936,subnet-0003aacf876f0fac1,securityGroupIds=sg-097730877ae293efd" |
| 219 | +fi |
| 220 | + |
0 commit comments