Property Handler cannot be empty - The following resource(s) failed to create - aws-cloudformation

Am trying to create lamda function using cloudformation. While creating stack am getting error like below. I have pasted my template below. Please help me where am doing mistake.
**20:01:50 UTC+0550 ROLLBACK_IN_PROGRESS AWS::CloudFormation::Stack MyStack The
following resource(s) failed to create: [Lamdafuntion]. . Rollback
requested by user.
20:01:50
UTC+0550 CREATE_FAILED AWS::Lambda::Function Lamdafuntion Property
Handler cannot be empty.**
Template :
"Type": "AWS::Lambda::Function",
"Properties": {
"Description" : "Lambda Function",
"Code" : {
"S3Bucket" : "awstier1bucket"
"S3Key" : "code.py.zip"
},
"FunctionName" : lambda_function
"Handler" : "test.lambda_handler",
"Tags": [
{
"Key": "Name",
"Value": "LambdaTest"
}
]

I believe it's due to mismatch of function and handler name.
Generally it has to be constructed by combining file_name.function_name
I've tried the following and it worked:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "yeye",
"Resources": {
"my-resource-1": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Description": "Lambda Function",
"Role": "arn:aws:iam::account_number:role/lambda_basic_execution",
"Runtime": "python2.7",
"Code": {
"S3Bucket": "my_bucket",
"S3Key": "**lambda_script**.py.zip"
},
"FunctionName": "my_function",
"Handler": "**lambda_script.function_name**"
}
}
}
}
In the example above I am uploading file named lambda_script.py which contains code with the following function defined:
def function_name(event, context):
"""
code
more code
"""
Which makes handler name lambda_script.function_name

Related

Unable to parse template language expression 'encodeURIComponent([parameters('table_storage_name')])'

Hey I am doing a CI/CD deployment for a logic app, I have a table storage where I store some data, I have two table storage for test and prod environment. I created a parameter called *table_storage_name" in ARM template :
"parameters": {
// ....
"connections_azuretables_1_externalid": {
"defaultValue": "/subscriptions/e5..../resourceGroups/myrg.../providers/Microsoft.Web/connections/azuretables-1",
"type": "String"
},
"table_storage_name": {
"defaultValue": "testdevops",
"type": "String"
}
}
The error comes from when I reference the parameter here in template.json file:
// ...
"Insert_Entity": {
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "ApiConnection",
"inputs": {
"body": {
"PartitionKey": "#body('Parse_JSON')?['name']",
"RowKey": "#body('Parse_JSON')?['last']"
},
"host": {
"connection": {
"name": "#parameters('$connections')['azuretables_1']['connectionId']"
}
},
"method": "post",
// problem occur after this line
"path": "/Tables/#{encodeURIComponent('[parameters('table_storage_name')]')}/entities"
}
}
but get this error:
InvalidTemplate: The template validation failed: 'The template action 'Insert_Entity' at line '1' and column '582' is not valid: "Unable to parse template language expression 'encodeURIComponent([parameters('table_storage_name')])': expected token 'Identifier' and actual 'LeftSquareBracket'.".'.
I tried escaping the quote with a backslash like: encodeURIComponent(\'[parameters('table_storage_name')]\') or encodeURIComponent('[parameters(''table_storage_name'')]') but all of them raise an error. How can I reference a paramter inside encodeURIComponent in an ARM template ?
As discussed in the comments. credits: #marone
"path": "/Tables/#{encodeURIComponent(parameters('table_storage_name'))}/entities"
Found the solution from this link https://platform.deloitte.com.au/articles/preparing-azure-logic-apps-for-cicd
but here are the steps to reference a parameter logic app:
create an ARM parameter table_storage_name_armparam in template.json, in order to use it's value to reference the value of the ARM parameter (yes it's confusing but follow along you'll understand):
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"table_storage_name_armparam": {
"type": "String"
}
},
"variables": {},
"resources": [
{
......
}
Now in the logic app parameter value (in the bottom of json file) create the logic app parameter table_storage_name and the value of this parameter will be the ARM parameter created in step 1:
.......
"parameters": {
"$connections": {
"value": {
"azuretables": {
"connectionId": "[parameters('connections_azuretables_externalid')]",
"connectionName": "azuretables",
"id": "/subscriptions/xxxxx-xxxx-xxxx-xxxxxxxx/providers/Microsoft.Web/locations/francecentral/managedApis/azuretables"
}
}
},
"table_storage_name": {
"value": "[parameters('table_storage_name_armparam')]"
}
}
}
}
]
}
finally, reference the logic app parameter value as follow:
"path": "/Tables/#{encodeURIComponent(parameters('table_storage_name'))}/entities"

AWS CloudFormation Parameter values specified for a template which does not require them

I porting code from ruby to Python for CloudFormation stack creation projects. Below is a stack that I just keep getting 'Parameter values specified for a template which does not require them.'
This really doesn't tell me anything.
I have checked the json against the schemas and all was ok, and checked against the stack created by the original code and it matches, so can someone see an issue here, or at least point me in the right direction.
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "EcsStack-5ad0c44afbf508d0b5a158df0da307fca33f5f63",
"Outputs": {
"marc1EcsCluster": {
"Value": {
"Ref": "marc1EcsCluster"
}
},
"marc1EcsClusterArn": {
"Value": {
"Fn::GetAtt": [
"marc1EcsCluster",
"Arn"
]
}
}
},
"Parameter": {
"Vpc": {
"Description": "VPC ID",
"Type": "String"
}
},
"Resources": {
"CloudFormationDummyResource": {
"Metadata": {
"Comment": "Resource to update stack even if there are no changes",
"GitCommitHash": "5ad0c44afbf508d0b5a158df0da307fca33f5f63"
},
"Type": "AWS::CloudFormation::WaitConditionHandle"
},
"marc1EcsCluster": {
"Type": "AWS::ECS::Cluster"
}
},
"Transform": "AWS::Serverless-2016-10-31"
}
As more general advice, the CloudFormation Linter will catch these errors with messages like:
E1001: Top level item Parameter isn't valid
template.json:19

Launch an ec2 instance with cloudformation

I am trying to launch an ec2 instance using cloudformation.I created this json template but I get error Template format error: At least one Resources member must be defined.
{
"Type" : "AWS::EC2::Instance",
"Properties" : {
"ImageId" : "ami-08ddb3f251a88cf33",
"InstanceType" : "t2.micro ",
"KeyName" : "Stagingkey",
"LaunchTemplate" : {
"LaunchTemplateId" : "jen1",
"LaunchTemplateName" : "Launchinstance",
"Version":"V1"
},
"SecurityGroupIds" : [ "sg-055f49a32efd4238b" ],
"SecurityGroups" : [ "jenkins_group" ],
}
}
What am I doing wrong?
Is there any other template for ap-south-1 region which I could use? Any help would be appreciated.
The error says it all: At least one Resources member must be defined.
The major sections of a template are:
Parameters
Mappings
Resources
Outputs
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "My Stack",
"Resources": {
"MyInstance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": "ami-08ddb3f251a88cf33",
"InstanceType": "t2.micro ",
"KeyName": "Stagingkey",
"LaunchTemplate": {
"LaunchTemplateId": "jen1",
"LaunchTemplateName": "Launchinstance",
"Version": "V1"
},
"SecurityGroupIds": [
"sg-055f49a32efd4238b"
],
"SecurityGroups": [
"jenkins_group"
]
}
}
}
}
You'll need to test it. For example, it is unlikely that you will define both SecurityGroupIds and SecurityGroups.
All the properties you have entered are properties of an EC2 resource, which you need to declare. You have no resources block/a logical name for you resource, like so:
"Resources": {
"MyTomcatName": {
"Type": "AWS::EC2::Instance",
"Properties": {
[...]

cloudformation template- validation error

I need to specify sns topic as a target using cloud formation template.
JobFailedAlert is the name of the sns topic.
I am have this template rule.json and I am getting the error
Error:
Template validation error: Template error: instance of Fn::GetAtt references undefined resource SNSTopic
Template:
{
"Resources": {
"Rule": {
"Type" : "AWS::Events::Rule",
"Properties" : {
"Description" : "create a sns alert when a batch job changes state to failed",
"EventPattern" : {
"detail-type": [
"Batch Job State Change"
],
"source": [
"aws.batch"
],
"detail": {
"jobQueue": [
"arn:aws:batch:us-east-1:************:job-queue/testbatchjobqueue"
],
"status": [
"FAILED"
]
}
},
"Name" : "alertonfailedbatchjobs2",
"State" : "Enabled",
"Targets": [
{
"Arn": { "Ref": "SNS Topic" },
"Id": "JobFailedAlert"
}
}
}
}
}
It may be that the rule is being created before the SNS topic. Try making sure that the SNS topic is created first by using DependsOn, such as:
"Rule": {
DependsOn: TheSNSTopic
...
}

Error when creating a blob storage output for Stream Analytics using Powershell

I'm trying to create a Stream Analytics blob storage output using powershell.
This is the command I'm using:
New-AzureRMStreamAnalyticsOutput -ResourceGroupName $ResourceGroupName -JobName $JobName –File soutput.json" -Force
and the output.json file looks like this:
{
"name": "test",
"properties": {
"datasource": {
"type": "Microsoft.Storage/Blob",
"properties": {
"storageAccounts": ["testStorage"],
"container": "testContainer",
"pathPattern": "",
"accountName": "testStorage",
"accountKey": "storage-key"
}
}
}
}
And I'm getting this error:
New-AzureRMStreamAnalyticsOutput : HTTP Status Code: BadRequest
Error Code: BadRequest
Error Message: The JSON provided in the request body is invalid.
Error converting value "testStorage" to type 'Microsoft.Streaming.Service.Contracts.CSMResourceProvider.BlobConnectionInfo'.
Path 'properties.storageAccounts[0]', line 8, position 106.
What should be in the storageAccounts property?
What should be in the storageAccounts property?
We need to set storageAccounts property:
"StorageAccounts": [
{
"AccountKey": "storagekey",
"AccountName": "storageaccount"
}
]
Property "Serialization" need to be included in the output json file.Please have a try to use output.json file as following. It works correctly for me.
{
"Name": "S3PSAJobOutPut",
"Properties": {
"DataSource": {
"Properties": {
"Container": "s3psaoutput",
"PathPattern": "",
"StorageAccounts": [
{
"AccountKey": "storagekey",
"AccountName": "storageaccount"
}
]
},
"Type": "Microsoft.Storage/Blob"
},
"Serialization": {
"Properties": {
"Encoding": "UTF8",
"Format": "LineSeparated"
},
"Type": "Json"
}
}
}