Skip to content

Commit f5f73bb

Browse files
committed
feat(vms): implement SNS topic command to trigger Lambda that stops the VM after initialization
1 parent 2c426cb commit f5f73bb

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

packages/actions/src/helpers/vm.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,24 @@ export const vmBootstrapCommand = (bucketName: string): Array<string> => [
7373
* Return the list of Node environment (and packages) installation plus artifact caching for contribution verification.
7474
* @param zKeyPath <string> - the path to zKey artifact inside AWS S3 bucket.
7575
* @param potPath <string> - the path to ptau artifact inside AWS S3 bucket.
76+
* @param snsTopic <string> - the SNS topic ARN.
7677
* @returns <Array<string>> - the array of commands to be run by the EC2 instance.
7778
*/
78-
export const vmDependenciesAndCacheArtifactsCommand = (zKeyPath: string, potPath: string): Array<string> => [
79+
export const vmDependenciesAndCacheArtifactsCommand = (
80+
zKeyPath: string,
81+
potPath: string,
82+
snsTopic: string,
83+
): Array<string> => [
7984
"#!/bin/bash",
8085
"sudo yum update -y",
8186
"sudo yum install -y nodejs",
8287
"npm install -g snarkjs",
8388
`aws s3 cp s3://${zKeyPath} /var/tmp/genesisZkey.zkey`,
8489
`aws s3 cp s3://${potPath} /var/tmp/pot.ptau`,
8590
"wget https://github.com/BLAKE3-team/BLAKE3/releases/download/1.4.0/b3sum_linux_x64_bin -O /var/tmp/blake3.bin",
86-
"chmod +x /var/tmp/blake3.bin"
91+
"chmod +x /var/tmp/blake3.bin",
92+
"INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)",
93+
`aws sns publish --topic-arn ${snsTopic} --message "$INSTANCE_ID"`
8794
]
8895

8996
/**
@@ -164,6 +171,10 @@ export const createEC2Instance = async (
164171
{
165172
Key: "Name",
166173
Value: ec2InstanceTag
174+
},
175+
{
176+
Key: "Initialized",
177+
Value: "false"
167178
}
168179
]
169180
}

packages/backend/.default.env

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ AWS_CEREMONY_BUCKET_POSTFIX="-ph2-ceremony"
1616
AWS_AMI_ID="ami-022e1a32d3f742bd8"
1717
# The EC2 instance role to access S3
1818
AWS_ROLE_ARN="YOUR-AWS-ROLE-ARN"
19+
# The SNS topic ARN to publish notifications
20+
AWS_SNS_TOPIC_ARN="YOUR-AWS-SNS-TOPIC-ARN"
1921

2022
### GENERIC ###
2123
### These configs are generic and not tied to Firebase or AWS services.

packages/backend/src/functions/ceremony.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import {
3030
getFinalContribution,
3131
htmlEncodeCircuitData,
3232
createEC2Client,
33-
uploadFileToBucketNoFile
33+
uploadFileToBucketNoFile,
34+
getAWSVariables
3435
} from "../lib/utils"
3536
import { LogLevel } from "../types/enums"
3637

@@ -148,10 +149,14 @@ export const setupCeremony = functions
148149
// Get EC2 client.
149150
const ec2Client = await createEC2Client()
150151

152+
// Get AWS variables.
153+
const { snsTopic } = getAWSVariables()
154+
151155
// Prepare dependencies and cache artifacts command.
152156
const vmCommands = vmDependenciesAndCacheArtifactsCommand(
153157
`${bucketName}/${circuit.files?.initialZkeyStoragePath!}`,
154-
`${bucketName}/${circuit.files?.potStoragePath!}`
158+
`${bucketName}/${circuit.files?.potStoragePath!}`,
159+
snsTopic
155160
)
156161

157162
printLog(vmCommands.join("\n"), LogLevel.DEBUG)

packages/backend/src/lib/utils.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ export const getAWSVariables = (): any => {
400400
!process.env.AWS_ACCESS_KEY_ID ||
401401
!process.env.AWS_SECRET_ACCESS_KEY ||
402402
!process.env.AWS_ROLE_ARN ||
403-
!process.env.AWS_AMI_ID
403+
!process.env.AWS_AMI_ID ||
404+
!process.env.AWS_SNS_TOPIC
404405
)
405406
logAndThrowError(COMMON_ERRORS.CM_WRONG_CONFIGURATION)
406407

@@ -409,7 +410,8 @@ export const getAWSVariables = (): any => {
409410
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
410411
region: process.env.AWS_REGION || "us-east-1",
411412
roleArn: process.env.AWS_ROLE_ARN!,
412-
amiId: process.env.AWS_AMI_ID!
413+
amiId: process.env.AWS_AMI_ID!,
414+
snsTopic: process.env.AWS_SNS_TOPIC!
413415
}
414416
}
415417

0 commit comments

Comments
 (0)