-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws.ts
44 lines (37 loc) · 1.45 KB
/
aws.ts
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
import AWS from "aws-sdk";
import execa from "execa";
export const AWS_REGION = "us-east-2";
export const CLOUDFRONT_DISTRIBUTION_ID = "E2O86FEUUQ9OA4";
export const API_GATEWAY_ID = "mxypxe62o6";
export const S3_BUCKET_NAME = "prairielearn-marketing-prod";
export const LAMBDA_EXECUTION_ROLE =
"arn:aws:iam::769954110362:role/marketing-api-lambda-role";
export const getAwsAccountId = async () => {
const sts = new AWS.STS({ region: AWS_REGION });
const { Account: accountId } = await sts.getCallerIdentity().promise();
if (!accountId) {
throw new Error(`Could not determine account ID`);
}
return accountId;
};
export const getEcrRegistryUrl = async () => {
// ECR registries are tied to account IDs. Determine the account ID dynamically
// based on the credentials we're running with.
const accountId = await getAwsAccountId();
return `${accountId}.dkr.ecr.${AWS_REGION}.amazonaws.com`;
};
export const loginToEcr = async () => {
const ecr = new AWS.ECR({ region: AWS_REGION });
const authData = await ecr.getAuthorizationToken().promise();
const token = authData.authorizationData?.[0].authorizationToken;
if (!token) {
throw new Error(`Could not get ECR login token`);
}
const [user, password] = Buffer.from(token, "base64").toString().split(":");
const ecrRegistryUrl = await getEcrRegistryUrl();
await execa(
"docker",
["login", "--username", user, "--password-stdin", ecrRegistryUrl],
{ input: password }
);
};