def template(**kwargs): """ Args: Returns: template Template() """ template = Template() release = template.add_parameter( Parameter('ReleaseNewAlias', Type="String", AllowedValues=['Yes', 'No'], Default='No')) release_condition = template.add_condition( 'ReleaseAlias', {'ReleaseAlias': Equals(Ref(release), 'Yes')}) function = template.add_resource(lambda_function(**kwargs)) version = template.add_resource( Version('LambdaVersion', FunctionName=GetAtt(function, 'Arn'))) alias = template.add_resource( Alias('LambdaAlias', Name='prod', DependsOn=[release_condition], Description=Sub(f'Alias to version ${{{version.title}.Arn}}'), FunctionName=Ref(function), FunctionVersion=Ref(version))) template.add_output(object_outputs(function, True)) template.add_output(object_outputs(version, True)) return template
def getFunctionAlias( path: str , prefix: str , arn: str , stackname: str , stage: str ) -> Alias: ''' Returns the Alias if present ''' name = "".join([prefix, stackname, stage]) return Alias( toAlphanum(name) + "Alias" , Description = "Automatic Generated Alias" , FunctionName = arn , FunctionVersion = "$LATEST" , Name = name )
def add_versioned_lambda( template, deployment_id, function, ): environment = function.properties.setdefault("Environment", Environment(Variables={})) environment.Variables["X__DO_NOT_USE__DEPLOYMENT_ID"] = deployment_id function = template.add_resource(function) (is_odd_deployment, is_even_deployment) = add_double_sided_condition( template, f"{function.title}DeploymentIdParityOdd", Equals(determine_parity(deployment_id), "ODD"), ) version_a = template.add_resource( Version( f"{function.title}VersionA", FunctionName=GetAtt(function, "Arn"), Condition=is_odd_deployment, )) version_b = template.add_resource( Version( f"{function.title}VersionB", FunctionName=GetAtt(function, "Arn"), Condition=is_even_deployment, )) version_number = If( is_odd_deployment, GetAtt(version_a, "Version"), GetAtt(version_b, "Version"), ) alias = template.add_resource( Alias( f"{function.title}Alias", FunctionName=GetAtt(function, "Arn"), FunctionVersion=version_number, Name="latest", )) return function, alias
Timeout=Ref(timeout))) function_policy = t.add_resource( PolicyType("FunctionPolicy", PolicyDocument={ "Id": "FunctionPolicy", "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": ["arn:aws:logs:*:*:*"] }] }, PolicyName="function-policy", Roles=[Ref(function_role)])) alias = t.add_resource( Alias("LambdaAlias", Description="Cosmos Alias", FunctionName=Ref(aws_lambda), FunctionVersion="$LATEST", Name=Ref(env))) print(t.to_json())
"S3_BUCKET": Ref(questionsBucketName), "S3_CONFIG": Ref(configFolder), "S3_FOLDER": Ref(questionsFolder) } ), MemorySize=Ref(memorySize), Timeout=Ref(timeout), Code=Code( ZipFile="$LATEST" ), )) alias = template.add_resource(Alias( "LambdaAlias", Description="Cosmos Alias", FunctionName=Ref(efficientLearningFunction), FunctionVersion="$LATEST", Name=Ref(env) )) template.add_output(Output( "EfficientLearningFunctionArn", Export=Export("efficient-learning-function-arn"), Value=GetAtt(efficientLearningFunction, "Arn"), Description="Function to generate speech files" )) template.add_output(Output( "EfficientLearningRoleArn", Export=Export("efficient-learning-role-arn"),
FunctionName=Ref("LambdaFunction"), BatchSize=1)) t.add_resource( Permission("InvokeLambdaPermission", FunctionName=Join( ':', [GetAtt("LambdaFunction", "Arn"), Ref("LambdaEnv")]), Action="lambda:InvokeFunction", SourceArn=GetAtt("JsonNotificationReceiveQueue", "Arn"), Principal="sqs.amazonaws.com")) t.add_resource( Alias("GalileoBabelLambdaAlias", Description="Alias for the galileo babel lambda", FunctionName=Ref("LambdaFunction"), FunctionVersion="$LATEST", Name=Ref("LambdaEnv"))) t.add_resource( Queue("JsonNotificationReceiveQueue", QueueName=Sub("${LambdaEnv}-json-notification-inbound-queue"), RedrivePolicy=RedrivePolicy(deadLetterTargetArn=GetAtt( "JsonNotificationDLQ", "Arn"), maxReceiveCount=3))) t.add_resource( Queue( "JsonNotificationDLQ", QueueName=Sub("${LambdaEnv}-json-notification-inbound-dlq"), ))
def generate_template(self, template): alias = Parameter("LambdaEnvAlias", Default="int", Description="Alias used to reference the lambda", Type="String") template.add_parameter(aliasP) lambda_bucket = template.add_parameter( Parameter("LambdaBucket", Type="String", Default="go-lambda-hello-world", Description= "The S3 Bucket that contains the zip to bootstrap your " "lambda function")) s3_key = template.add_parameter( Parameter("S3Key", Type="String", Default="main.zip", Description= "The S3 key that references the zip to bootstrap your " "lambda function")) handler = template.add_parameter( Parameter( "LambdaHandler", Type="String", Default="event_handler.handler", Description="The name of the function (within your source code) " "that Lambda calls to start running your code.")) memory_size = template.add_parameter( Parameter( "LambdaMemorySize", Type="Number", Default="128", Description="The amount of memory, in MB, that is allocated to " "your Lambda function.", MinValue="128")) timeout = template.add_parameter( Parameter("LambdaTimeout", Type="Number", Default="300", Description= "The function execution time (in seconds) after which " "Lambda terminates the function. ")) lambda_function = template.add_resource( Function( "LambdaGoHelloWorld", Code=Code(S3Bucket="go-lambda-hello-world", S3Key=Ref(s3_key)), Description="Go function used to demonstate sqs integration", Handler=Ref(handler), Role=GetAtt("LambdaExecutionRole", "Arn"), Runtime="go1.x", MemorySize=Ref(memory_size), FunctionName="go-lambda-hello-world", Timeout=Ref(timeout))) alias = template.add_resource( Alias("GolLambdaAlias", Description="Alias for the go lambda", FunctionName=Ref(lambda_function), FunctionVersion="$LATEST", Name=Ref(alias))) dead_letter_queue = template.add_resource( Queue("GoLambdaDeadLetterQueue", QueueName=("golambdaqueue-dlq"), VisibilityTimeout=30, MessageRetentionPeriod=1209600, MaximumMessageSize=262144, DelaySeconds=0, ReceiveMessageWaitTimeSeconds=0)) go_helloworld_queue = template.add_resource( Queue("GoLambdaQueue", QueueName=("golambdaqueue"), VisibilityTimeout=1800, RedrivePolicy=RedrivePolicy(deadLetterTargetArn=GetAtt( dead_letter_queue, "Arn"), maxReceiveCount=3))) lambda_execution_role = template.add_resource( Role("LambdaExecutionRole", Policies=[ iam.Policy( PolicyName="GoFunctionRolePolicy", PolicyDocument=Policy(Statement=[ Statement(Effect=Allow, Action=[ Action("logs", "CreateLogGroup"), Action("logs", "CreateLogStream"), Action("logs", "PutLogEvents") ], Resource=["arn:aws:logs:*:*:*"]), Statement( Effect=Allow, Action=[ Action("sqs", "ChangeMessageVisibility"), Action("sqs", "DeleteMessage"), Action("sqs", "GetQueueAttributes"), Action("sqs", "ReceiveMessage") ], Resource=[GetAtt(go_helloworld_queue, "Arn")]) ])) ], AssumeRolePolicyDocument=Policy(Statement=[ Statement(Effect=Allow, Action=[AssumeRole], Principal=Principal("Service", ["lambda.amazonaws.com"])) ]))) template.add_resource( awslambda.Permission( 'QueueInvokePermission', FunctionName=GetAtt(lambda_function, 'Arn'), Action="lambda:InvokeFunction", Principal="sqs.amazonaws.com", SourceArn=GetAtt(go_helloworld_queue, 'Arn'), )) template.add_output( Output("LambdaHelloWorldQueue", Value=GetAtt(go_helloworld_queue, "Arn"), Export=Export("lambda-go-hello-world-queue"), Description="Arn of the queue")) template.add_output( Output("LambdaHelloWorlFunction", Value=Ref(alias), Export=Export("lambda-go-hello-world-function"), Description="Arn of the function"))