Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// If the GSI does not exist yet then add it.
const existingGSI = gsis.find(gsi => gsi.IndexName === connectionGSIName)
if (!existingGSI) {
const keySchema = [new KeySchema({ AttributeName: connectionAttributeName, KeyType: 'HASH' })]
if (sortField) {
keySchema.push(new KeySchema({ AttributeName: sortField.name, KeyType: 'RANGE' }))
}
gsis.push(new GlobalSecondaryIndex({
IndexName: connectionGSIName,
KeySchema: keySchema,
Projection: new Projection({
ProjectionType: 'ALL'
}),
ProvisionedThroughput: Fn.If(
ResourceConstants.CONDITIONS.ShouldUsePayPerRequestBilling,
Refs.NoValue,
{
ReadCapacityUnits: Fn.Ref(ResourceConstants.PARAMETERS.DynamoDBModelTableReadIOPS),
WriteCapacityUnits: Fn.Ref(ResourceConstants.PARAMETERS.DynamoDBModelTableWriteIOPS)
}
) as any,
}))
}
// If the attribute definition does not exist yet, add it.
const attributeDefinitions = table.Properties.AttributeDefinitions as AttributeDefinition[]
const existingAttribute = attributeDefinitions.find(attr => attr.AttributeName === connectionAttributeName)
if (!existingAttribute) {
attributeDefinitions.push(new AttributeDefinition({
AttributeName: connectionAttributeName,
AttributeType: 'S'
role = (name: string, region: string): any => {
return new IAM.Role({
RoleName: Fn.If(
ResourceConstants.CONDITIONS.HasEnvironmentParameter,
Fn.Join('-', [
FunctionResourceIDs.FunctionIAMRoleName(name, true), // max of 64. 64-10-26-28 = 0
Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'), // 26
Fn.Ref(ResourceConstants.PARAMETERS.Env), // 10
]),
Fn.Join('-', [
FunctionResourceIDs.FunctionIAMRoleName(name, false), // max of 64. 64-26-38 = 0
Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'), // 26
])
),
AssumeRolePolicyDocument: {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Principal: {
if (primaryPartitionKeyName === ks[0].AttributeName) {
// This is an LSI.
// Add the new secondary index and update the table's attribute definitions.
tableResource.Properties.LocalSecondaryIndexes = append(
tableResource.Properties.LocalSecondaryIndexes,
new LocalSecondaryIndex(baseIndexProperties)
)
} else {
// This is a GSI.
// Add the new secondary index and update the table's attribute definitions.
tableResource.Properties.GlobalSecondaryIndexes = append(
tableResource.Properties.GlobalSecondaryIndexes,
new GlobalSecondaryIndex({
...baseIndexProperties,
ProvisionedThroughput: Fn.If(
ResourceConstants.CONDITIONS.ShouldUsePayPerRequestBilling,
Refs.NoValue,
{
ReadCapacityUnits: Fn.Ref(ResourceConstants.PARAMETERS.DynamoDBModelTableReadIOPS),
WriteCapacityUnits: Fn.Ref(ResourceConstants.PARAMETERS.DynamoDBModelTableWriteIOPS)
}
) as any,
})
)
}
const existingAttrDefSet = new Set(tableResource.Properties.AttributeDefinitions.map(ad => ad.AttributeName));
for (const attr of attrDefs) {
if (!existingAttrDefSet.has(attr.AttributeName)) {
tableResource.Properties.AttributeDefinitions.push(attr);
}
}
}
public makeAppSyncApiKey(apiKeyConfig: Transformer.ApiKeyConfig) {
let expirationDays = 7;
if (apiKeyConfig && apiKeyConfig.apiKeyExpirationDays) {
expirationDays = apiKeyConfig.apiKeyExpirationDays;
}
const expirationDateInSeconds = 60 /* s */ * 60 /* m */ * 24 /* h */ * expirationDays; /* d */
const nowEpochTime = Math.floor(Date.now() / 1000);
return new AppSync.ApiKey({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
Description: apiKeyConfig && apiKeyConfig.description ? apiKeyConfig.description : undefined,
Expires: Fn.If(
ResourceConstants.CONDITIONS.APIKeyExpirationEpochIsPositive,
Fn.Ref(ResourceConstants.PARAMETERS.APIKeyExpirationEpoch),
nowEpochTime + expirationDateInSeconds
),
}).condition(ResourceConstants.CONDITIONS.ShouldCreateAPIKey);
}
public makeAppSyncApiKey(apiKeyConfig: Transformer.ApiKeyConfig) {
let expirationDays = 7;
if (apiKeyConfig && apiKeyConfig.apiKeyExpirationDays) {
expirationDays = apiKeyConfig.apiKeyExpirationDays;
}
const expirationDateInSeconds = 60 /* s */ * 60 /* m */ * 24 /* h */ * expirationDays; /* d */
const nowEpochTime = Math.floor(Date.now() / 1000);
return new AppSync.ApiKey({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
Description: apiKeyConfig && apiKeyConfig.description ? apiKeyConfig.description : undefined,
Expires: Fn.If(
ResourceConstants.CONDITIONS.APIKeyExpirationEpochIsPositive,
Fn.Ref(ResourceConstants.PARAMETERS.APIKeyExpirationEpoch),
nowEpochTime + expirationDateInSeconds
),
}).condition(ResourceConstants.CONDITIONS.ShouldCreateAPIKey);
}
private joinWithEnv(separator: string, listToJoin: any[]) {
return Fn.If(
ResourceConstants.CONDITIONS.HasEnvironmentParameter,
Fn.Join(
separator,
[
...listToJoin,
Fn.Ref(ResourceConstants.PARAMETERS.Env)
]
),
Fn.Join(
separator,
listToJoin
)
)
}
function joinWithEnv(separator: string, listToJoin: any[]) {
return Fn.If(
ResourceConstants.CONDITIONS.HasEnvironmentParameter,
Fn.Join(separator, [...listToJoin, Fn.Ref(ResourceConstants.PARAMETERS.Env)]),
Fn.Join(separator, listToJoin)
);
}
export function syncLambdaIAMRole({ name, region }: { name: string; region?: string }) {
public makeAppSyncAPI() {
return new AppSync.GraphQLApi({
Name: Fn.If(
ResourceConstants.CONDITIONS.HasEnvironmentParameter,
Fn.Join('-', [Fn.Ref(ResourceConstants.PARAMETERS.AppSyncApiName), Fn.Ref(ResourceConstants.PARAMETERS.Env)]),
Fn.Ref(ResourceConstants.PARAMETERS.AppSyncApiName)
),
AuthenticationType: 'API_KEY',
});
}
export function syncLambdaArnResource({ name, region }: { name: string; region?: string }) {
const env = 'env;';
const substitutions = {};
if (referencesEnv(name)) {
substitutions[env] = Fn.Ref(ResourceConstants.PARAMETERS.Env);
}
return Fn.If(
ResourceConstants.CONDITIONS.HasEnvironmentParameter,
Fn.Sub(lambdaArnKey(name, region), substitutions),
Fn.Sub(lambdaArnKey(removeEnvReference(name), region), {})
);
}
export function lambdaArnKey(name: string, region?: string) {
public makeEnvironmentConditions() {
return {
[ResourceConstants.CONDITIONS.HasEnvironmentParameter]:
Fn.Not(Fn.Equals(Fn.Ref(ResourceConstants.PARAMETERS.Env), ResourceConstants.NONE))
}
}