Skip to content

Commit

Permalink
feat(vms): implement SNS topic command to trigger Lambda that stops t…
Browse files Browse the repository at this point in the history
…he VM after initialization
  • Loading branch information
ctrlc03 committed Jun 29, 2023
1 parent 2c426cb commit f5f73bb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
15 changes: 13 additions & 2 deletions packages/actions/src/helpers/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,24 @@ export const vmBootstrapCommand = (bucketName: string): Array<string> => [
* Return the list of Node environment (and packages) installation plus artifact caching for contribution verification.
* @param zKeyPath <string> - the path to zKey artifact inside AWS S3 bucket.
* @param potPath <string> - the path to ptau artifact inside AWS S3 bucket.
* @param snsTopic <string> - the SNS topic ARN.
* @returns <Array<string>> - the array of commands to be run by the EC2 instance.
*/
export const vmDependenciesAndCacheArtifactsCommand = (zKeyPath: string, potPath: string): Array<string> => [
export const vmDependenciesAndCacheArtifactsCommand = (
zKeyPath: string,
potPath: string,
snsTopic: string,
): Array<string> => [
"#!/bin/bash",
"sudo yum update -y",
"sudo yum install -y nodejs",
"npm install -g snarkjs",
`aws s3 cp s3://${zKeyPath} /var/tmp/genesisZkey.zkey`,
`aws s3 cp s3://${potPath} /var/tmp/pot.ptau`,
"wget https://github.com/BLAKE3-team/BLAKE3/releases/download/1.4.0/b3sum_linux_x64_bin -O /var/tmp/blake3.bin",
"chmod +x /var/tmp/blake3.bin"
"chmod +x /var/tmp/blake3.bin",
"INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)",
`aws sns publish --topic-arn ${snsTopic} --message "$INSTANCE_ID"`
]

/**
Expand Down Expand Up @@ -164,6 +171,10 @@ export const createEC2Instance = async (
{
Key: "Name",
Value: ec2InstanceTag
},
{
Key: "Initialized",
Value: "false"
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/.default.env
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ AWS_CEREMONY_BUCKET_POSTFIX="-ph2-ceremony"
AWS_AMI_ID="ami-022e1a32d3f742bd8"
# The EC2 instance role to access S3
AWS_ROLE_ARN="YOUR-AWS-ROLE-ARN"
# The SNS topic ARN to publish notifications
AWS_SNS_TOPIC_ARN="YOUR-AWS-SNS-TOPIC-ARN"

### GENERIC ###
### These configs are generic and not tied to Firebase or AWS services.
Expand Down
9 changes: 7 additions & 2 deletions packages/backend/src/functions/ceremony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
getFinalContribution,
htmlEncodeCircuitData,
createEC2Client,
uploadFileToBucketNoFile
uploadFileToBucketNoFile,
getAWSVariables
} from "../lib/utils"
import { LogLevel } from "../types/enums"

Expand Down Expand Up @@ -148,10 +149,14 @@ export const setupCeremony = functions
// Get EC2 client.
const ec2Client = await createEC2Client()

// Get AWS variables.
const { snsTopic } = getAWSVariables()

// Prepare dependencies and cache artifacts command.
const vmCommands = vmDependenciesAndCacheArtifactsCommand(
`${bucketName}/${circuit.files?.initialZkeyStoragePath!}`,
`${bucketName}/${circuit.files?.potStoragePath!}`
`${bucketName}/${circuit.files?.potStoragePath!}`,
snsTopic
)

printLog(vmCommands.join("\n"), LogLevel.DEBUG)
Expand Down
6 changes: 4 additions & 2 deletions packages/backend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ export const getAWSVariables = (): any => {
!process.env.AWS_ACCESS_KEY_ID ||
!process.env.AWS_SECRET_ACCESS_KEY ||
!process.env.AWS_ROLE_ARN ||
!process.env.AWS_AMI_ID
!process.env.AWS_AMI_ID ||
!process.env.AWS_SNS_TOPIC
)
logAndThrowError(COMMON_ERRORS.CM_WRONG_CONFIGURATION)

Expand All @@ -409,7 +410,8 @@ export const getAWSVariables = (): any => {
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
region: process.env.AWS_REGION || "us-east-1",
roleArn: process.env.AWS_ROLE_ARN!,
amiId: process.env.AWS_AMI_ID!
amiId: process.env.AWS_AMI_ID!,
snsTopic: process.env.AWS_SNS_TOPIC!
}
}

Expand Down

0 comments on commit f5f73bb

Please sign in to comment.