|
| 1 | +Deployment with the CDK |
| 2 | +======================= |
| 3 | + |
| 4 | +Sleeper is deployed with the AWS Cloud Development Kit (CDK). This can be done either with scripts as described in |
| 5 | +the [deployment guide](../deployment-guide.md#scripted-deployment), or by using the CDK directly. This document covers |
| 6 | +deployment using the CDK CLI directly. |
| 7 | + |
| 8 | +### Uploading artefacts to AWS |
| 9 | + |
| 10 | +Some jars and Docker images must be uploaded to AWS before you can deploy an instance of Sleeper. We have a CDK app |
| 11 | +`SleeperArtefactsCdkApp` which creates an S3 bucket and ECR repositories to hold these artefacts, but does not |
| 12 | +upload the artefacts. You can also include this in your own CDK app with `SleeperArtefacts`. You can use our tools to |
| 13 | +upload the artefacts as a separate step, or implement your own way to do this that may be specific to your Maven and |
| 14 | +Docker repositories. |
| 15 | + |
| 16 | +The scripted deployment uploads the jars from the local `scripts/jars` directory within the Git repository. The Docker |
| 17 | +images are either built from the local `scripts/docker` directory or pulled from a remote repository if that is |
| 18 | +configured. You could replicate that behaviour yourself with the script `scripts/deploy/uploadArtefacts.sh`, or use our |
| 19 | +Java classes `SyncJars` and `UploadDockerImagesToEcr`, or implement your own way to upload these artefacts. |
| 20 | + |
| 21 | +As part of `scripts/build/build.sh`, the jars are built and output to `scripts/jars`, and the Docker builds are prepared |
| 22 | +in separate directories for each Docker image under `scripts/docker`. You can also use |
| 23 | +our [publishing tools](../development/publishing.md) to prepare the artefacts. |
| 24 | + |
| 25 | +It's important to upload artefacts from within AWS to avoid lengthy uploads into AWS. Usually this is done from an EC2 |
| 26 | +instance. |
| 27 | + |
| 28 | +#### `uploadArtefacts.sh` |
| 29 | + |
| 30 | +This script can upload artefacts to an existing CDK deployment. You can either pass in the deployment ID that you used |
| 31 | +for the CDK deployment, or pass in an instance properties file for an instance that is configured to use that artefacts |
| 32 | +deployment. In the latter case, Docker images will only be uploaded if they are required with your instance |
| 33 | +configuration. Run `uploadArtefacts.sh --help` for details. |
| 34 | + |
| 35 | +By default, the artefacts deployment ID should match the instance ID. Alternatively, you can set the deployment ID in |
| 36 | +the instance property [`sleeper.artefacts.deployment`](../usage/properties/instance/user/common.md). |
| 37 | + |
| 38 | +Here's an example with a CDK command to create an artefacts deployment, and a call to the script to upload all artefacts |
| 39 | +to that deployment: |
| 40 | + |
| 41 | +```bash |
| 42 | +DEPLOYMENT_ID=my-deployment |
| 43 | +cdk deploy --all -c id=$DEPLOYMENT_ID -a "java -cp ./scripts/jars/cdk-<version>.jar sleeper.cdk.SleeperArtefactsCdkApp" |
| 44 | +./scripts/deploy/uploadArtefacts.sh --id $DEPLOYMENT_ID |
| 45 | +``` |
| 46 | + |
| 47 | +#### Direct upload |
| 48 | + |
| 49 | +If you prefer to implement this yourself, details of Docker images to be uploaded can be |
| 50 | +found [here](/docs/deployment/docker-images.md). That document includes details of how to build and push the images to |
| 51 | +ECR, as it is done by the automated scripts. |
| 52 | + |
| 53 | +You'll also need to create an S3 bucket for jars, and upload the contents of the `scripts/jars` directory to it. That |
| 54 | +directory is created during a build, or during installation of a published version. The jars S3 bucket needs to have |
| 55 | +versioning enabled so we can tie a CDK deployment to specific versions of each jar. |
| 56 | + |
| 57 | +When not using an artefacts CDK deployment, you can set the instance properties `sleeper.jars.bucket` |
| 58 | +and `sleeper.ecr.repository.prefix` instead of `sleeper.artefacts.deployment`. |
| 59 | + |
| 60 | +### Including Sleeper in your CDK app |
| 61 | + |
| 62 | +Sleeper supports deployment as part of your own CDK app, either as its own stack or as a nested stack under your stack. |
| 63 | +If you have published Sleeper to a Maven repository as described in the [publishing guide](../development/publishing.md) |
| 64 | +you can add the Sleeper CDK module as a Maven artefact like this: |
| 65 | + |
| 66 | +```xml |
| 67 | +<dependency> |
| 68 | + <groupId>sleeper</groupId> |
| 69 | + <artifactId>cdk</artifactId> |
| 70 | + <version>version.number.here</version> |
| 71 | +</dependency> |
| 72 | +``` |
| 73 | + |
| 74 | +Use the class `SleeperInstance` to add instances of Sleeper to your app. To load instance and table properties from |
| 75 | +the local file system you can use `DeployInstanceConfiguration.fromLocalConfiguration`. Here's an example: |
| 76 | + |
| 77 | +```java |
| 78 | +Stack stack = Stack.Builder.create(app, "MyStack") |
| 79 | + .stackName("my-stack") |
| 80 | + .env(environment) |
| 81 | + .build(); |
| 82 | +SleeperInstanceConfiguration myInstanceConfig = SleeperInstanceConfiguration.fromLocalConfiguration( |
| 83 | + workingDir.resolve("my-instance/instance.properties")); |
| 84 | +SleeperInstance.createAsNestedStack(stack, "MyInstance", |
| 85 | + NestedStackProps.builder() |
| 86 | + .description("My instance") |
| 87 | + .build(), |
| 88 | + SleeperInstanceProps.builder(myInstanceConfig, s3Client, dynamoClient) |
| 89 | + .deployPaused(false) |
| 90 | + .build()); |
| 91 | +``` |
| 92 | + |
| 93 | +### Using the CDK CLI |
| 94 | + |
| 95 | +To deploy a Sleeper instance to AWS with the CDK, you need an [instance configuration](instance-configuration.md) and |
| 96 | +a [suitable environment](environment-setup.md). The artefacts will need to be uploaded as described in the section |
| 97 | +above. You can either use the instance ID as the deployment ID for the artefacts, or you can point to your artefacts |
| 98 | +deployment with the instance property `sleeper.artefacts.deployment`. |
| 99 | + |
| 100 | +You can use the same CDK apps used by the automated scripts, or your own CDK configuration. We'll give examples with the |
| 101 | +CDK apps used by the automated scripts. The following commands will deploy a Sleeper instance: |
| 102 | + |
| 103 | +```bash |
| 104 | +INSTANCE_PROPERTIES=/path/to/instance.properties |
| 105 | +SCRIPTS_DIR=./scripts # This is from the root of the Sleeper Git repository |
| 106 | +VERSION=$(cat "$SCRIPTS_DIR/templates/version.txt") |
| 107 | +cdk deploy --all -c propertiesfile=$INSTANCE_PROPERTIES -c newinstance=true -a "java -cp $SCRIPTS_DIR/jars/cdk-$VERSION.jar sleeper.cdk.SleeperCdkApp" |
| 108 | +``` |
| 109 | + |
| 110 | +To avoid having to explicitly give approval for deploying all the stacks, you can add "--require-approval never" to the |
| 111 | +command. |
| 112 | + |
| 113 | +If you'd like to include data generation for system tests, use the system test CDK app instead. |
| 114 | + |
| 115 | +```bash |
| 116 | +INSTANCE_PROPERTIES=/path/to/instance.properties |
| 117 | +SCRIPTS_DIR=./scripts # This is from the root of the Sleeper Git repository |
| 118 | +VERSION=$(cat "$SCRIPTS_DIR/templates/version.txt") |
| 119 | +cdk deploy --all -c propertiesfile=$INSTANCE_PROPERTIES -c newinstance=true -a "java -cp $SCRIPTS_DIR/jars/system-test-$VERSION-utility.jar sleeper.systemtest.cdk.SystemTestApp" |
| 120 | +``` |
| 121 | + |
| 122 | +#### Tear down |
| 123 | + |
| 124 | +If the artefacts and the Sleeper instance are each deployed in their own CDK app, with `SleeperArtefactsCdkApp` and |
| 125 | +`SleeperCdkApp`, you can tear down an instance of Sleeper either by deleting the CloudFormation stacks, or with the CDK |
| 126 | +CLI. You may need to delete the Sleeper instance before deleting the artefacts used to deploy it. Here's an example: |
| 127 | + |
| 128 | +```bash |
| 129 | +INSTANCE_PROPERTIES=/path/to/instance.properties |
| 130 | +ID=my-instance-id |
| 131 | +SCRIPTS_DIR=./scripts # From the root of the Sleeper Git repository |
| 132 | +VERSION=$(cat "$SCRIPTS_DIR/templates/version.txt") |
| 133 | + |
| 134 | +cdk destroy --all -c propertiesfile=$INSTANCE_PROPERTIES -c validate=false -a "java -cp $SCRIPTS_DIR/jars/cdk-$VERSION.jar sleeper.cdk.SleeperCdkApp" |
| 135 | +cdk destroy --all -c id=$ID -a "java -cp $SCRIPTS_DIR/jars/cdk-$VERSION.jar sleeper.cdk.SleeperArtefactsCdkApp" |
| 136 | +``` |
0 commit comments