Skip to content

Commit

Permalink
feat: introduce deployment settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Shelomentsev committed Dec 19, 2023
1 parent 64e1e39 commit 850ca61
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
52 changes: 40 additions & 12 deletions mgmt-lambda/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '@aws-sdk/client-lambda'
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager'
import type { AuthSettings } from './model/AuthSettings'
import type { DeploymentSettings } from './model/DeploymentSettings'

const REGION = 'us-east-1'

Expand All @@ -29,26 +30,31 @@ export async function handler(
const authSettings = await getAuthSettings()
console.info(authSettings)

const authorization = event.headers['Authorization']
const authorization = event.headers['authorization']
if (authorization !== authSettings.token) {
const notAuthResponse = {
statusCode: 401,
}
callback(null, notAuthResponse)
}

//TODO load data from AWS Secret
const userInput = {
LAMBDA_NAME: '',
CF_DISTR_ID: '',
let deploymentSettings: DeploymentSettings
try {
deploymentSettings = loadDeploymentSettings()
} catch (error) {
const wrongEnv = {
statusCode: 500,
body: {
error: error,
},
}
callback(null, wrongEnv)
}
const lambdaFunctionName = userInput.LAMBDA_NAME
const cloudFrontDistrId = userInput.CF_DISTR_ID

const path = event.rawPath
console.info(`path = ${path}`)
if (path.startsWith('/update')) {
handleUpdate(cloudFrontDistrId, lambdaFunctionName)
handleUpdate(deploymentSettings)
} else if (path.startsWith('/status')) {
}

Expand Down Expand Up @@ -89,11 +95,33 @@ async function getAuthSettings(): Promise<AuthSettings> {
}
}

async function handleUpdate(cloudFrontDistributionId: string, lambdaFunctionName: string) {
function loadDeploymentSettings(): DeploymentSettings {
const cfDistributionId = process.env.CFDistributionId
if (!cfDistributionId) {
throw new Error('No CloudFront distribution Id')
}
const lambdaFunctionName = process.env.LambdaFunctionArn
if (!lambdaFunctionName) {
throw new Error('No lambda function name')
}
const lambdaFunctionArn = process.env.LambdaFunctionArn
if (!lambdaFunctionArn) {
throw new Error('No lambda function ARN')
}

const settings: DeploymentSettings = {
CFDistributionId: cfDistributionId,
LambdaFunctionArn: lambdaFunctionArn,
LambdaFunctionName: lambdaFunctionName,
}
return settings
}

async function handleUpdate(settings: DeploymentSettings) {
console.info(`Going to upgrade Fingerprint Pro function association at CloudFront distbution.`)
console.info(`Lambda function: ${lambdaFunctionName}. CloudFront ID: ${cloudFrontDistributionId}`)
console.info(`Settings: ${settings}`)

const latestFunctionArn = await getLambdaLatestVersionArn(lambdaFunctionName)
const latestFunctionArn = await getLambdaLatestVersionArn(settings.LambdaFunctionName)
if (!latestFunctionArn) {
return publishJobFailure('No lambda versions')
}
Expand All @@ -103,7 +131,7 @@ async function handleUpdate(cloudFrontDistributionId: string, lambdaFunctionName
return publishJobSuccess()
}

updateCloudFrontConfig(cloudFrontDistributionId, lambdaFunctionName, latestFunctionArn)
updateCloudFrontConfig(settings.CFDistributionId, settings.LambdaFunctionName, latestFunctionArn)
}

async function updateCloudFrontConfig(
Expand Down
5 changes: 5 additions & 0 deletions mgmt-lambda/model/DeploymentSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface DeploymentSettings {
CFDistributionId: string
LambdaFunctionArn: string
LambdaFunctionName: string
}

0 comments on commit 850ca61

Please sign in to comment.