Skip to content

Commit

Permalink
fix(vms): add tags on EC2 creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Jun 27, 2023
1 parent 8f3dc42 commit 0347289
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
2 changes: 2 additions & 0 deletions packages/actions/src/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const finalContributionIndex = "final"
export const verificationKeyAcronym = "vkey"
// The acronym for Verifier smart contract.
export const verifierSmartContractAcronym = "verifier"
// The tag for ec2 instances
export const ec2InstanceTag = "p0tionec2instance"

/**
* Commonly used terms.
Expand Down
19 changes: 16 additions & 3 deletions packages/actions/src/helpers/ec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
StartInstancesCommand,
StopInstancesCommand,
TerminateInstancesCommand,
DescribeInstancesCommand,
EC2Client
EC2Client,
RunInstancesCommandInput
} from "@aws-sdk/client-ec2"
import {
GetCommandInvocationCommand,
Expand All @@ -15,6 +15,7 @@ import {
} from "@aws-sdk/client-ssm"
import dotenv from "dotenv"
import { P0tionEC2Instance } from "../types"
import { ec2InstanceTag } from "./constants"

dotenv.config()

Expand Down Expand Up @@ -133,7 +134,7 @@ export const createEC2Instance = async (
volumeSize: number
): Promise<P0tionEC2Instance> => {
// create the params
const params = {
const params: RunInstancesCommandInput = {
ImageId: amiId,
InstanceType: instanceType, // to be determined programmatically
MaxCount: 1,
Expand All @@ -153,6 +154,18 @@ export const createEC2Instance = async (
},
},
],
// tag the resource
TagSpecifications: [
{
ResourceType: "instance",
Tags: [
{
Key: "Name",
Value: ec2InstanceTag
}
]
}
]
}

// create command
Expand Down
3 changes: 2 additions & 1 deletion packages/actions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export {
solidityVersion,
finalContributionIndex,
verificationKeyAcronym,
verifierSmartContractAcronym
verifierSmartContractAcronym,
ec2InstanceTag
} from "./helpers/constants"
export {
extractPrefix,
Expand Down
7 changes: 3 additions & 4 deletions packages/actions/test/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import { fakeCeremoniesData } from "../data/samples"
* Create a new S3 Client object
* @returns <S3Client | boolean> an S3 client if the credentials are set, false otherwise
*/
const getS3Client = (): any => {
export const getS3Client = (): any => {
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY)
// throw new Error("Missing AWS credentials, please add them in the .env file")
return {
success: false,
client: null
Expand Down Expand Up @@ -86,8 +85,8 @@ export const deleteBucket = async (bucketName: string): Promise<boolean> => {
})
const response = await s3.send(command)

if (response.$metadata.httpStatusCode !== 200) return false
return true
if (response.$metadata.httpStatusCode === 200 || response.$metadata.httpStatusCode === 204) return true
return false
} catch (error: any) {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/functions/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ParticipantStatus,
ParticipantContributionStep,
formatZkeyIndex,
getZkeyStorageFilePath
getZkeyStorageFilePath,
} from "@p0tion/actions"
import { getCeremonyCircuits, getDocumentById } from "../lib/utils"
import { COMMON_ERRORS, logAndThrowError, makeError, printLog, SPECIFIC_ERRORS } from "../lib/errors"
Expand Down Expand Up @@ -171,7 +171,7 @@ export const createBucket = functions
// Allow objects to be public
const publicBlockResponse = await S3.send(publicBlockCommand)
// Check response.
if (publicBlockResponse.$metadata.httpStatusCode === 200)
if (publicBlockResponse.$metadata.httpStatusCode === 204)
printLog(
`The AWS S3 bucket ${data.bucketName} has been set with the PublicAccessBlock disabled.`,
LogLevel.LOG
Expand Down

0 comments on commit 0347289

Please sign in to comment.