Adding parameters to aws lambda events using templates - amazon-web-services

How do I add a (path) parameter to lambda function events using cloud formation templates?
Strangely using:
DeleteItem:
Type: Api
Properties:
Path: /item/{id}
Method: delete
Request:
Parameters:
Paths:
id: true
works using aws-sam-cli. However when I try to deploy using cloud formation it says that property Request is not defined. I got this request idea from the serverless docs but seems to only work locally. I can't find documentation on how to do this in templates so any help would be greatly appreciated.

The Serverless Framework uses its own syntax, which is different from SAM (although can be compiled down to SAM or raw CloudFormation).
You can find the SAM specification here.
It's not explicit, but all you need to do is use the {path-name} syntax. Adding Request/Parameters is not required (or supported).
For example:
Ratings:
Type: AWS::Serverless::Function
Properties:
Handler: ratings.handler
Runtime: python3.6
Events:
Api:
Type: Api
Properties:
Path: /ratings/{id}
Method: get
Would give you the an event with:
event.pathParameters.id == 'whatever-was-put-in-the-id-position'
(A long example can be found here: https://github.com/1Strategy/redirect/blob/master/redirect.yaml)

Related

Add multiple parameters to path parameters AWS SAM

I have a dynamodb table that has a primary key of CompanyName and sort Key of CognitoUserID. I have a REST API and created an update method to update a user in my table. The issue I am having is that in my Yaml template I need to provide both the primary and sort key for my path parameters but I am only able to provide one. The code below is of my YAML template
Type: AWS::Serverless::Function
Properties:
CodeUri: cloudPortalFunctions/
Handler: app.updateUserProfile
Layers:
- !Ref updateUserProfileDepLayer
Runtime: nodejs14.x
Architectures:
- x86_64
Events:
updateUserProfile:
Type: Api
Properties:
Path: /updateUserProfile/{cognitoUserID}
Method: PUT
This is my update method in my YAML file
I would like to be able to add CompanyName to the path. Maybe to look like this
Path: /updateUserProfile/{companyName}{cognitoUserID}
I have tried this How can i use multiple path parameters from serverless framework but none of it is accepted in my vs code and the AWS documentation does not help either
Is there a way to do this?
I just needed to delete the stack and deploy it from scratch

Referencing serverless stack name after pseudo parameters plugin deprecation

I'm wondering what is the correct way to reference AWS Cloudformation pseudo parameters in a serverless.yml now that pseudo parameter plugin has been deprecated.
All pseudo parameters are not available with the dollar sign syntax (e.g. ${aws:stackName} is not) in a similar manner as ${aws:region} is, for example. The serverless documentation on pseudo parameters is very short and I am not sure I fully understand it. I have tried to use Ref: "AWS::StackName", but when I try to generate an output
Fn::Sub:
- "${Stack}-someOutputResourceName"
- Stack:
Ref: "AWS::StackName"
, I get an error with [...]/Fn::Sub/1/Stack] 'null' values are not allowed in templates.
The pseudo-plugin page claims that
All functionalities as provided by this plugin are now supported by Serverless Framework natively
If this is true, how should I go about using pseudo-parameters?
It seems that while the above method does not work, I am able to use the pseudo variable directly without using Ref:
Name: !Sub "${AWS::StackName}-someOutputResourceName"

AWS-Lambda: Unable to add another event, unhelpful GenerateChangeSet failure message

I am trying to add another path to AWS Lambda that has been generated by code star.
I have tried to add the following events (in different commits) with the same failure behavior.
HelloWorld:
Type: AWS::Serverless::Function
Properties:
Events:
API:
Type:
Properties:
Path: /{proxy+}
Method: ANY
HelloWorld:
Type: AWS::Serverless::Function
Properties:
Events:
Decks_GetEvent:
Type: Api
Properties:
Path: /decks
Method: get
GenerateChangeSet step in code pipeline fails with the following unhelpful message:
Action execution failed
Failed to create change set. Current status: FAILED
When going to 'Details' Link of GenerateChangeSet step. It just takes me to Stack view without any particular stack or execution event selected.
What are the steps to troubleshoot this? Any suggestions on what could be causing this?
If it doesn't want to create a changeset, you presumably have a syntax error in your document. However, since you've not shared the CloudFormation template, that is hard to determine.
If you want to troubleshoot this, you can try to manually create a change set (that might provide more error information), or you can use cfn-lint to do some static code analysis on your CloudFormation template.

AWS Lambda Rest API: A sibling ({id}) of this resource already has a variable path part -- only one is allowed Unable to create resource at path

I'm particular new to Lambda and to AWS in general. I'm trying to setup a simple REST API Service with Lambda. I've used CloudFormat and CodePipeline to have a simple Express app.
I'm trying to figure out why during the deployment phase, during ExecuteChangeSet I have this error:
Errors found during import: Unable to create resource at path '/stations/{stationId}/allowedUsers': A sibling ({id}) of this resource already has a variable path part -- only one is allowed Unable to create resource at path '/stations/{stationId}/allowedUsers/{userId}': A sibling ({id}) of this resource already has a variable path part -- only one is allowed
This is what I have inside the template.yml
Events:
AllowedUsers:
Type: Api
Properties:
Path: /stations/{stationId}/allowedUsers
Method: get
AddAllowedUsers:
Type: Api
Properties:
Path: /stations/{stationId}/allowedUsers
Method: post
DeleteAllowedUsers:
Type: Api
Properties:
Path: /stations/{stationId}/allowedUsers/{userId}
Method: delete
GetAllowedUser:
Type: Api
Properties:
Path: /stations/{stationId}/allowedUsers/{userId}
Method: get
I searched a bit for this error but I'm not sure how to solve it.
For me, the issue was different from what is described in the GitHub issue Bryan mentioned.
I was using two different parameter names. Finishing the refactoring and using a single id name fixed the issue.
Example:
DeleteAllowedUsers:
Type: Api
Properties:
Path: /stations/{stationId}/allowedUsers/{id}
Method: delete
GetAllowedUser:
Type: Api
Properties:
Path: /stations/{stationId}/allowedUsers/{userId}
Method: get
Here is the walk around for this problem. It was posted on github by pettyalex.
link :https://github.com/serverless/serverless/issues/3785
You might encounter this issue when updating a variable path while using serverless ( and serverless.yaml ) to provision the AWS gatewayApi, here is a walk-around:
comment out the endpoint function to remove it completely
uncomment and deploy again

Use private ips in google dataflow job being created via google provided template

I'm trying to set up a dataflow job via deployment manager using the google provided template Cloud_PubSub_to_Avro.
To do this I had to register dataflow as a type provider, like this:
resources:
- name: 'register-dataflow'
action: 'gcp-types/deploymentmanager-v2beta:deploymentmanager.typeProviders.insert'
properties:
name: 'dataflow'
descriptorUrl: 'https://dataflow.googleapis.com/$discovery/rest?version=v1b3'
options:
inputMappings:
- fieldName: Authorization
location: HEADER
value: >
$.concat("Bearer ", $.googleOauth2AccessToken())
Then I created my job template that looks something like
resources:
- name: "my-topic-to-avro"
type: 'my-project-id/dataflow:dataflow.projects.locations.templates.launch'
properties:
projectId: my-project-id
gcsPath: "gs://dataflow-templates/latest/Cloud_PubSub_to_Avro"
jobName: "my-topic-to-avro"
location: "europe-west1"
parameters:
inputTopic: "projects/my-project-id/topics/my-topic"
outputDirectory: "gs://my-bucket/avro/my-topic/"
avroTempDirectory: "gs://my-bucket/avro/tmp/my-topic/"
Now I'm trying to understand how I can tell my job to not use public ips. From this it looks like I need to set --usePublicIps=false but I can't figure out where to place this parameter, or if this is even possible.
A possible workaround I found here would be to remove the access-config, but again I haven't been able to figure out how to do this, if at all possible.
Is what I'm trying to do possible through provided templates or will I have to use the dataflow API?
Any help appreciated.