281 lines (246 loc) · 10.4 KB
/
ecs_prefect_agent.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
name: 1 Initial - ECS Prefect agent
on:
workflow_dispatch:
inputs:
cpu:
description: CPU for the agent
required: true
default: '512'
type: choice
options: ['256', '512', '1024', '2048', '4096']
memory:
description: Memory for the agent
required: true
default: '1024'
type: choice
options: ['512', '1024', '2048', '4096', '5120', '6144', '7168', '8192']
s3_path:
description: S3 path in the format bucket/path
required: true
default: 'prefect-orion/prod'
block_name:
description: Name of the S3 and ECS blocks
required: true
default: 'prod'
prefect-version:
description: Prefect version for flows
required: true
default: '2.*'
type: string
aws-region:
description: AWS Region
required: true
default: 'us-east-1'
type: string
env:
PROJECT: dataflowops
ECS_CLUSTER: dataflowops
BLOCK: ${{ github.event.inputs.block_name }}
AWS_REGION: ${{ github.event.inputs.aws-region }}
PREFECT_VERSION: ${{ github.event.inputs.prefect-version }}
jobs:
ecr-repo:
name: ECR Repo & Image
runs-on: ubuntu-latest
outputs:
image: ${{ steps.build-image.outputs.image }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Generate Markdown Summary
run: echo "AWS ECS deployment with commit hash $GITHUB_SHA" >> $GITHUB_STEP_SUMMARY
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
mask-aws-account-id: 'no'
- name: Create new ECR repository using AWS CloudFormation
uses: aws-actions/aws-cloudformation-github-deploy@v1.1.0
with:
name: "${{ env.PROJECT }}-ecr"
template: infrastructure/ecr_repository.yml
parameter-overrides: "RepositoryName=${{ env.PROJECT }}"
no-fail-on-empty-changeset: "1"
- name: Flow deployment finished
run: echo "ECR repository created at $(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_STEP_SUMMARY
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
export IMAGE="$ECR_REGISTRY/$PROJECT:$IMAGE_TAG"
docker build -t $IMAGE .
docker push $IMAGE
echo "image=$IMAGE" >> $GITHUB_OUTPUT
- name: Flow deployments finished
run: echo "ECR image ${{ steps.build-image.outputs.image }} built at $(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_STEP_SUMMARY
prefect-agent-ecs:
name: ECS Cluster & Prefect Agent
runs-on: ubuntu-latest
needs: ecr-repo
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
mask-aws-account-id: 'no'
- name: Add Prefect Cloud Secrets to SSM Parameter Store (needed for container in ECS task)
id: aws
run: |
aws ssm put-parameter --type SecureString --name PREFECT_API_URL --value ${{ secrets.PREFECT_API_URL }} --overwrite
aws ssm put-parameter --type SecureString --name PREFECT_API_KEY --value ${{ secrets.PREFECT_API_KEY }} --overwrite
export AWS_ACCOUNT_ID="$(aws sts get-caller-identity --query Account --output text)"
echo "accountid=$AWS_ACCOUNT_ID" >> $GITHUB_OUTPUT
# echo ::set-output name=accountid::$AWS_ACCOUNT_ID
- name: Deploy to ECS with AWS CloudFormation
uses: aws-actions/aws-cloudformation-github-deploy@v1.1.0
with:
name: ${{ env.PROJECT }}
template: infrastructure/ecs_cluster_prefect_agent.yml
capabilities: CAPABILITY_NAMED_IAM
parameter-overrides: "cpu=${{ github.event.inputs.cpu }},memory=${{ github.event.inputs.memory }},project=${{ env.PROJECT }},cluster=${{ env.ECS_CLUSTER }},image=${{ needs.ecr-repo.outputs.image }},awsaccountid=${{steps.aws.outputs.accountid}},region=${{ env.AWS_REGION }}"
- name: ECS Service Deployment finished
run: echo "ECS Service Deployment finished at $(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_STEP_SUMMARY
- name: Generate task definition
id: task-definition
run: aws ecs describe-task-definition --task-definition $PROJECT --query taskDefinition > task-definition.json
- name: Upload task definition as artifact
uses: actions/upload-artifact@v3
with:
name: ECS task definition
path: task-definition.json
- name: Summary
run: echo 'AWS ECS agent successfully deployed and ready to run your flows! :rocket:' >> $GITHUB_STEP_SUMMARY
blocks:
name: Prefect Blocks & S3 Upload
runs-on: ubuntu-latest
needs: ecr-repo
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Python 3.9
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Python dependencies
run: |
pip install prefect==$PREFECT_VERSION
pip install .
prefect block register -m prefect_aws.ecs
- name: Prefect Cloud login
run: |
prefect config set PREFECT_API_KEY=${{ secrets.PREFECT_API_KEY }}
prefect config set PREFECT_API_URL=${{ secrets.PREFECT_API_URL }}
- name: AWS credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
mask-aws-account-id: 'no'
- name: Create Blocks & Upload to S3
id: aws_ecs_ecs_blocks
run: |
export AWS_ACCOUNT_ID="$(aws sts get-caller-identity --query Account --output text)"
cat <<EOF > aws_ecs_blocks.py
from prefect.filesystems import S3
from prefect_aws.ecs import ECSTask, AwsCredentials
id_ = "${{ secrets.AWS_ACCESS_KEY_ID }}"
key_ = "${{ secrets.AWS_SECRET_ACCESS_KEY }}"
path_ = "${{ github.event.inputs.s3_path }}"
img_ = "${{ needs.ecr-repo.outputs.image }}"
block_ = "$BLOCK"
cluster_ = "${{ env.ECS_CLUSTER }}"
cpu_ = "${{ github.event.inputs.cpu }}"
memory_ = "${{ github.event.inputs.memory }}"
aws_acc_id = "$AWS_ACCOUNT_ID"
exec_role = f"arn:aws:iam::{aws_acc_id}:role/$PROJECT_ecs_execution_role"
task_role = f"arn:aws:iam::{aws_acc_id}:role/$PROJECT_ecs_task_role"
aws_creds = AwsCredentials(aws_access_key_id=id_, aws_secret_access_key=key_)
aws_creds.save(block_, overwrite=True)
s3 = S3(bucket_path=path_, aws_access_key_id=id_, aws_secret_access_key=key_)
s3.save(block_, overwrite=True)
ecs = ECSTask(
aws_credentials=aws_creds,
image=img_,
cpu=cpu_,
memory=memory_,
stream_output=True,
configure_cloudwatch_logs=True,
cluster=cluster_,
execution_role_arn=exec_role,
task_role_arn=task_role,
)
ecs.save(block_, overwrite=True)
EOF
python aws_ecs_blocks.py
- name: Blocks creation finished
run: echo "AWS and ECS blocks built at $(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_STEP_SUMMARY
- name: Upload to S3 - maintenance flow
id: maintenance
run: prefect deployment build utilities/maintenance.py:maintenance -n $PROJECT -sb s3/$BLOCK -q $PROJECT -v $GITHUB_SHA -a -t maintenance --interval 10
list-flows:
name: List Prefect flows
runs-on: ubuntu-latest
needs: blocks
outputs:
prefect_flows: ${{ steps.set-matrix.outputs.prefect_flows }}
steps:
- name: Checkout
uses: actions/checkout@v3
- id: set-matrix
run: echo "prefect_flows=$(ls flows/*.py | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT
# todo may need to remove double str in "\n"
# run: echo "::set-output name=prefect_flows::$(ls flows/*.py | jq -R -s -c 'split("\n")[:-1]')"
deploy:
name: Deploy
needs: list-flows
runs-on: ubuntu-latest
strategy:
matrix:
flows: ${{ fromJson(needs.list-flows.outputs.prefect_flows) }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Python 3.9
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Python dependencies
run: |
pip install prefect==$PREFECT_VERSION
pip install .
- name: Prefect Cloud login
run: |
prefect config set PREFECT_API_KEY=${{ secrets.PREFECT_API_KEY }}
prefect config set PREFECT_API_URL=${{ secrets.PREFECT_API_URL }}
- name: AWS credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
mask-aws-account-id: 'no'
- name: Deploy flows to S3
id: build
run: |
FLOW_NAME=$(basename ${{ matrix.flows }} .py)
prefect deployment build ${{ matrix.flows }}:$FLOW_NAME -sb s3/$BLOCK -q $PROJECT -v $GITHUB_SHA -a -t $PROJECT -n $PROJECT -o $FLOW_NAME.yaml -ib ecs-task/$BLOCK --skip-upload
prefect deployment build ${{ matrix.flows }}:$FLOW_NAME -sb s3/$BLOCK -q $PROJECT -v $GITHUB_SHA -a -t $PROJECT -n $PROJECT-local --skip-upload
echo "flow_manifest=$FLOW_NAME.yaml" >> $GITHUB_OUTPUT
# echo ::set-output name=flow_manifest::$FLOW_NAME.yaml
- name: Upload YAML deployment manifest as artifact
uses: actions/upload-artifact@v3
with:
name: Deployment YAML manifests
path: ${{ steps.build.outputs.flow_manifest }}