How can I deploy websocket with serverless framework - amazon-web-services

I have below configuration on serverless.yml. But it doesn't deploy websocket connection. I wonder what could be wrong with my configuration. I have followed this instruction: https://www.serverless.com/framework/docs/providers/aws/events/websocket/
service:
name: ${opt:componentName}-api
plugins:
- '#hewmen/serverless-plugin-typescript'
provider:
name: aws
runtime: nodejs12.x
region: ap-southeast-2
websocketsApiName: custom-websockets-api-name
websocketApiRouteSelectionExpression: $request.body.action
stackName: ${opt:stage}-${self:service.name}
iamRoleStatements:
- Effect: Allow
Action:
- logs:Create*
- logs:Get*
Resource: "*"
- Effect: Allow
Action:
- dynamodb:*
Resource: "*"
functions:
wsHandler:
handler: src/websocketLambda.handleWebSocket
name: ${self:provider.stackName}-ws
evnets:
- websocket: $default
The output of serverless deploy is:
Serverless: Stack update finished...
Service Information
service: device-api-transactions-api
stage: dev
region: ap-southeast-2
stack: dev-device-api-transactions-api
resources: 6
api keys:
None
endpoints:
None
functions:
wsHandler: dev-device-api-transactions-api-ws
layers:
None
Serverless: Removing old service artifacts from S3...
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.

Related

How to deploy serverless file to AWS from bitbucket pipeline

I am trying to deploy a serverless template using bitbucket pipeline. I have provided the sls deploy command as such sls deploy --stage ${environmentName} --cloudformationStackDynamoDB $dynamodbEventStoreStackName --componentName ${componentName} --partName ${partName} --eventBusName $eventBusName --region ${awsRegion} || exit
But I get this error when the pipeline reaches the step to deploy the serverless.yml file "Error:Cannot resolve serverless.yml: "service" property is not accessible (configured behind variables which cannot be resolved at this stage)"
Below is the content of serverless.yml
service:
name: ${opt:stage}-${opt:componentName}-${opt:partName}-stream
plugins:
- "#hewmen/serverless-plugin-typescript"
- serverless-plugin-resource-tagging
#- serverless-plugin-optimize
#- serverless-offline
# - serverless-plugin-warmup
# custom:
# # Enable warmup on all functions (only for production and staging)
# warmup:
# enabled: true
provider:
name: aws
runtime: nodejs16.x
stackName: ${self:service.name}
iamRoleStatements:
- Effect: Allow
Action:
- logs:Create*
- logs:Get*
Resource: "*"
- Effect: Allow
Action:
- dynamodb:*
Resource: "*"
- Effect: Allow
Action:
- events:PutEvents
Resource: "*" #TODO - apply pattern */eventbus-name
environment:
ENVIRONMENT: ${opt:stage}
COMPONENT_NAME: ${opt:componentName}
PART_NAME: ${opt:partName}
EVENTBUS_NAME: ${opt:eventBusName} #TODO - supply this as part of the CICD build
stackTags:
COMPONENT_NAME: ${opt:componentName}
STAGE: ${opt:stage}
functions:
default:
handler: src/lambda.streamHandler
name: ${self:provider.stackName}
events:
- stream: ${cf:${opt:cloudformationStackDynamoDB}.DynamoDBTableEventsStreamArn}
timeout: 30
I don't understand why the error says "service" property is not accessible because it is defined in the template file itself.
Any help would be appreciated.
Thanks!

CREATE_FAILED: IamRoleLambdaExecution (AWS::IAM::Role)

How I fix this one.
Error:
serverless.yml
provider:
name: aws
runtime: nodejs12.x
memorySize: 256
stage: ${opt:stage, 'dev'}
region: eu-west-1
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb.PutItem
Resource:
- arn:aws:dynamodb:#{AWS::Region}:#{AWS::AccountId}:table/AuctionsTable
I am new to aws services and serverless app development
when running sls deploy I am getting below error.
You have error in policy document, you used .(dot) in place of :(colon).
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:PutItem # use `:` here instead of `.`

Serverless deploys second function for S3 trigger

I have created AWS Lambda function to run when new fils in S3 at specific path is created, which works perfectly.
service: redshift
frameworkVersion: '2'
custom:
bucket: extapp
path_prefix: 'xyz'
database: ABC
schema: xyz_dbo
table_prefix: shipmentlog
user: admin
password: "#$%^&*(*&^%$%"
port: 5439
endpoint: "*********.redshift.amazonaws.com"
role: "arn:aws:iam::*****:role/RedshiftFileTransfer"
provider:
name: aws
runtime: python3.8
stage: prod
region: us-west-2
stackName: redshift-prod-copy
stackTags:
Service: "it"
lambdaHashingVersion: 20201221
memorySize: 128
timeout: 900
logRetentionInDays: 14
environment:
S3_BUCKET: ${self:custom.bucket}
S3_BUCKET_PATH_PREFIX: ${self:custom.path_prefix}
REDSHIFT_DATABASE: ${self:custom.database}
REDSHIFT_SCHEMA: ${self:custom.schema}
REDSHIFT_TABEL_PREFIX: ${self:custom.table_prefix}
REDSHIFT_USER: ${self:custom.user}
REDSHIFT_PASSWORD: ${self:custom.password}
REDSHIFT_PORT: ${self:custom.port}
REDSHIFT_ENDPOINT: ${self:custom.endpoint}
REDSHIFT_ROLE: ${self:custom.role}
iam:
role:
name: s3-to-redshift-copy
statements:
- Effect: Allow
Action:
- s3:GetObject
Resource: "arn:aws:s3:::${self:custom.bucket}/*"
functions:
copy:
handler: handler.run
events:
- s3:
bucket: ${self:custom.bucket}
event: s3:ObjectCreated:*
rules:
- prefix: ${self:custom.path_prefix}/
- suffix: .json
existing: true
package:
exclude:
- node_modules/**
- package*.json
- README.md
plugins:
- serverless-python-requirements
But when I deployed this function, there was also another function get deployed with name redshift-prod-custom-resource-existing-s3 which is Node.js function. I want to understand why this second function necessary for triggering primary lambda function when new file is creates in S3 bucket at specific path.
It's the serverless Framework's method of adding the trigger to call the lambda to the S3 bucket via a Custom Resource

serverless lambda#edge s3 bucket

I`m trying to upload lambda#edge with serverless but its not working, I cant see logs on the cloudwatch.
service: image-compress
frameworkVersion: '2'
plugins:
- serverless-bundle
provider:
name: aws
runtime: nodejs12.x
memorySize: 128
lambdaHashingVersion: '20201221'
stage: ${opt:stage, 'staging'}
environment:
ENV: ${self:provider.stage}
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:*"
Resource:
- "arn:aws:s3:::*"
functions:
image-reduce:
handler: handler.reducer
events:
- cloudFront:
eventType: origin-response
origin: s3://cropped-images-test2.s3.amazonaws.com/
isDefaultOrigin: true
custom:
bundle:
linting: false
packagerOptions:
scripts:
- npm install --arch=x64 --platform=linux sharp
This is the serverless yml, I can see this configuration on the cloudFront but I cant seem to get any logs when trying to receive images.
any help?

serverless create_domain - Failed to create custom domain

I am trying to use serverless-domain-manager to deploy serverless to custom domains on AWS but every time I run serverless create_domain I get the generic error "Failed to create custom domain". Here are the steps I've taken and the contents of my yml:
Registered domain on AWS
Set up a hosted zone in route 53
Created a certificate for *.mydomain.com in certificate manager in AWS
Created an iAM user with admin privileges
Run aws configure with iAM user keys
.yml
service: service-name
plugins:
- serverless-apigw-binary
- serverless-apigwy-binary
- serverless-content-encoding
- serverless-api-cloudfront
- serverless-plugin-tracing
- serverless-domain-manager
custom:
apigwBinary:
types:
- 'image/jpeg'
- 'text/html'
- 'text/css'
- 'application/javascript'
- 'application/x-javascript'
- '*/*'
contentEncoding:
minimumCompressionSize: 0
stage: ${opt:stage, self:provider.stage}
domains:
prod: api.mydomain.com
test: api-test.mydomain.com
dev: api-dev.mydomain.com
customDomain:
basePath: ""
domainName: ${self:custom.domains.${self:custom.stage}}
stage: "${self:custom.stage}"
createRoute53Record: true
certificateArn: "cert-arn"
hostedZoneId: "zone-id"
provider:
name: aws
runtime: nodejs12.x
endpointType: regional
region: ap-southeast-2
memorySize: 3008
timeout: 300
tracing: true
iamRoleStatements:
- Effect: 'Allow'
Action:
- 'xray:PutTraceSegments'
- 'xray:PutTelemetryRecords'
- 'lambda:InvokeFunction'
Resource:
- '*'
package:
exclude:
- node_modules/aws-sdk/**
- .vscode/**
- bin/**
- obj/**
functions:
api:
warmup: true
handler: index.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'
Every time I run serverless create_domain I get the same generic error with no extra info.
Thanks
As Erico pointed out you get a bit more info with SLS_DEBUG. It could be that you have an issue with domain certificate and that debug message would tell you that.
In my case it seemed to be a cert problem as it was in a different AWS region so sls create_domain couldn't find it. Overall it's been a rather confusing experience.
Check out the endpointType option:
customDomain:
...
endpointType="regional"
https://github.com/amplify-education/serverless-domain-manager/issues/46
https://aws.amazon.com/about-aws/whats-new/2017/11/amazon-api-gateway-supports-regional-api-endpoints/
In my case, my hosted zone is in another AWS account, so createRoute53Record: true failed to create the route53 record and the hence the generic error.
Changing to createRoute53Record: false fixed the error for me.