Unable to deploy AWS Lambda C# blank function - amazon-web-services

Unable to deploy AWS Lambda C# blank function
I believe that the following line is failing:
dotnet lambda package
I am getting started with AWS and Lambda. My platform is a bash shell on Windows 10. I have installed AWS CLI v2, and .NET Core v3.1, both appear to be installed ok:
$ aws --version
aws-cli/2.1.13 Python/3.7.9 Windows/10 exe/AMD64 prompt/off
$ dotnet --version
3.1.404
I am trying to walk through the blank function sample:
https://docs.aws.amazon.com/lambda/latest/dg/samples-blank.html
Instructions for C#:
https://github.com/awsdocs/aws-lambda-developer-guide/blob/main/sample-apps/blank-csharp/README.md
Per the instructions, I added the following to my .aws/config file:
cli_binary_format = raw-in-base64-out
The first step was successful, the S3 bucket was created:
$ ./1-create-bucket.sh
make_bucket: lambda-artifacts-1234567890123456
$ aws s3 ls
2020-12-30 14:30:25 lambda-artifacts-1234567890123456
But the second step, deployment, fails:
$ ./2-deploy.sh
Could not execute because the specified command or file was not found.
Possible reasons for this include:
* You misspelled a built-in dotnet command.
* You intended to execute a .NET Core program, but dotnet-lambda does not exist.
* You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
The contents of that shell script are:
#!/bin/bash
set -eo pipefail
ARTIFACT_BUCKET=$(cat bucket-name.txt)
cd src/blank-csharp
dotnet lambda package
cd ../../
aws cloudformation package --template-file template.yml --s3-bucket $ARTIFACT_BUCKET --output-template-file out.yml
aws cloudformation deploy --template-file out.yml --stack-name blank-csharp --capabilities CAPABILITY_NAMED_IAM
I assume that the error is reported by line 5 invoking dotnet, since the error report refers to dotnet.
I tried to install the Lambda tools in dotnet, but that failed:
$ dotnet tool install -g Amazon.Lambda.Tools
error NU1100: Unable to resolve 'amazon.lambda.tools (>= 0.0.0)' for '.NETCoreApp,Version=v3.1'.
error NU1100: Unable to resolve 'amazon.lambda.tools (>= 0.0.0)' for '.NETCoreApp,Version=v3.1/any'.
The tool package could not be restored.
Tool 'amazon.lambda.tools' failed to install. This failure may have been caused by:
You are attempting to install a preview release and did not use the --version option to specify the version.
A package by this name was found, but it was not a .NET Core tool.
The required NuGet feed cannot be accessed, perhaps because of an Internet connection problem.
You mistyped the name of the tool.
For more reasons, including package naming enforcement, visit https://aka.ms/failure-installing-tool
Similar result when trying to install the Lambda templates:
$ dotnet new -i Amazon.Lambda.Templates
Determining projects to restore...
C:\Users\Me\.templateengine\dotnetcli\v3.1.404\scratch\restore.csproj : error NU1100: Unable to resolve 'Amazon.Lambda.Templates (>= 0.0.0)' for '.NETCoreApp,Version=v1.0'.
Failed to restore C:\Users\Me\.templateengine\dotnetcli\v3.1.404\scratch\restore.csproj (in 179 ms).
I tried uninstalling .NET Core SDK v3.1 and repeating with .NET Core SDK v2.1, and also with .NET SDK 5.0, same results. I tried disabling anti-virus, no effect.
Should I have installed the AWS SDK? I did try, but yet more failure. I downloaded
awssdk.lambda.3.5.6.8.nupkg
from
https://www.nuget.org/packages/AWSSDK.Lambda/
and from the directory containing this nupkg file:
$ dotnet add package AWSSDK.Lambda
Could not find any project in 'C:\AwsSdkLambda\'.
I'd very much appreciate any suggestions.
Thanks,
Henry

Related

How to solve an AWS Lamba function deployment problem?

.. aaaand me again :)
This time with a very interesting problem.
Again AWS Lambda function, node.js 12, Javascript, Ubuntu 18.04 for local development, aws cli/aws sam/Docker/IntelliJ, everything is working perfectly in local and is time to deploy.
So I did set up an AWS account for tests, created and assigned an access key/secret and finally did try to deploy.
Almost at the end an error pop up aborting the deployment.
I'm showing the SAM cli version from a terminal, but the same happens with IntelliJ.
(of course I mask/change some names)
From a terminal I'm going where I have my local sandbox with the project and then :
$ sam deploy --guided
Configuring SAM deploy
======================
Looking for config file [samconfig.toml] : Not found
Setting default arguments for 'sam deploy'
=========================================
Stack Name [sam-app]: MyActualProjectName
AWS Region [us-east-1]: us-east-2
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
Confirm changes before deploy [y/N]: y
#SAM needs permission to be able to create roles to connect to the resources in your template
Allow SAM CLI IAM role creation [Y/n]: y
Save arguments to configuration file [Y/n]: y
SAM configuration file [samconfig.toml]: y
SAM configuration environment [default]:
Looking for resources needed for deployment: Not found.
Creating the required resources...
Successfully created!
Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-7qo1hy7mdu9z
A different default S3 bucket can be set in samconfig.toml
Saved arguments to config file
Running 'sam deploy' for future deployments will use the parameters saved above.
The above parameters can be changed by modifying samconfig.toml
Learn more about samconfig.toml syntax at
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
Error: Unable to upload artifact MyFunctionName referenced by CodeUri parameter of MyFunctionName resource.
ZIP does not support timestamps before 1980
$
I spent quite some time looking around for this problem but I found only some old threads.
In theory this problems was solved in 2018 ... but probably some npm libraries I had to use contains something old ... how in the world I fix this stuff ?
In one thread I found a kind of workaround.
In the file buildspec.yml somebody suggested to add AFTER the npm install :
ls $CODEBUILD_SRC_DIR
find $CODEBUILD_SRC_DIR/node_modules -mtime +10950 -exec touch {} ;
Basically the idea is to touch all the files installed after the npm install but still the error happens.
This my buildspec.yml file after the modification :
version: 0.2
phases:
install:
commands:
# Install all dependencies (including dependencies for running tests)
- npm install
- ls $CODEBUILD_SRC_DIR
- find $CODEBUILD_SRC_DIR/node_modules -mtime +10950 -exec touch {} ;
pre_build:
commands:
# Discover and run unit tests in the '__tests__' directory
- npm run test
# Remove all unit tests to reduce the size of the package that will be ultimately uploaded to Lambda
- rm -rf ./__tests__
# Remove all dependencies not needed for the Lambda deployment package (the packages from devDependencies in package.json)
- npm prune --production
build:
commands:
# Use AWS SAM to package the application by using AWS CloudFormation
- aws cloudformation package --template template.yml --s3-bucket $S3_BUCKET --output-template template-export.yml
artifacts:
type: zip
files:
- template-export.yml
I will continue to search but again I wonder if somebody here had this kind of problem and thus some suggestions/methodology about how to solve it.
Many many thanks !
Steve

Azure DevOps CICD build pipeline fails with error: specified command or file was not found

I have a CICD build pipeline in Azure Devops for building a .NET Core AWS API Gateway Serverless application. The Pipeline is using hosted Windows 2019. The step that fails is:
steps:
- task: AmazonWebServices.aws-vsts-tools.LambdaNETCoreDeploy.LambdaNETCoreDeploy#1
displayName: 'Build solution and generate CloudFormation template. '
inputs:
awsCredentials: 'AWS - Development (Infrastructure)'
regionName: 'ap-southeast-2'
command: deployServerless
packageOnly: true
packageOutputFile: '$(Build.ArtifactStagingDirectory)\serverless-output.yaml'
lambdaProjectPath: testapi/LCSApi.csproj
s3Bucket: 'api-dev-xxxxxxxx-s3'
s3Prefix: 'azure_devops_builds/lcs/'
additionalArgs: '-template serverless.template '
All I get from the error is the following:
Beginning Serverless Deployment
Performing package-only build of serverless application, output template will be placed in D:\a\1\a\serverless-output.yaml
"C:\Program Files\dotnet\dotnet.exe" lambda package-ci -ot D:\a\1\a\serverless-output.yaml --region ap-southeast-2 --s3-bucket api-dev-xxxxxx-s3 --s3-prefix azure_devops_builds/lcs/ --disable-interactive true -template serverless.template
Could not execute because the specified command or file was not found.
Possible reasons for this include:
* You misspelled a built-in dotnet command.
* You intended to execute a .NET Core program, but dotnet-lambda does not exist.
* You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
##[error]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1
Finishing: Build solution and generate CloudFormation template.
However, if I re-run the pipeline straight after this failure, it works fine. Additionally, it does not always fail with this error. Around 70-80% of the time the pipeline works fine. What could this be and how can i address it?
Can you try adding this before your step:
powershell: |
dotnet tool install --global Amazon.Lambda.Tools --version 3.1.1
dotnet tool update -g Amazon.Lambda.Tools

How to downgrade Terraform from Version 12 to 11 in Google Cloud Shell

I'm using Google CLoud Console to run terraform Scrit, I found that Cloud shell is already equiped with Terrafrom version 12.. My Google resources is only supported for Terraform version 11.. How do I downgrade Terraform from Version 12 to 11 in Google CLoud Shell
running all terraform commands in container hashicorp/terraform:0.11.14, that's what I am doing currently for old project. so it has no chance to upgrade terraform version any more in container.
TERRAFORM_IMAGE=hashicorp/terraform:0.11.14
TERRAFORM_CMD="docker run -ti --rm -w /app -v ${HOME}/.aws:/root/.aws -v ${HOME}/.ssh:/root/.ssh -v `pwd`:/app -w /app ${TERRAFORM_IMAGE}"
${TERRAFORM_CMD} init
${TERRAFORM_CMD} plan
Second, make sure you limit the terraform version in your codes. If not, you have the risk to run terraform with higher version (>0.12) and get tfstate file upgraded directly. It will be hard to roll back, unless you enable the version control when save *tfstate files.
terraform {
required_version = "<= 0.11.14"
}
The easiest way to manage your Terraform versions is the tfswitch tool.
You can install it from here: tfswitch
Usage
|--⫸ tfswitch
Use the arrow keys to navigate: ↓ ↑ → ←
? Select Terraform version:
▸ 0.11.12 *recent
0.11.13 *recent
0.11.14 *recent
0.12.19
↓ 0.12.18

AWS elastic beanstalk git deployment suddenly failing due to composer issue despite no changes to composer.json

I have a number of environments running in AWS Elastic Beanstalk. I deploy direct from git using git aws.push.
I use composer.json to install required php sdk's. I've not changed this file for a long time but it's suddenly started failing in all environments.
Output from the AWS logs is
+ echo 'Found composer.json file. Attempting to install vendors.'
Found composer.json file. Attempting to install vendors.
+ composer.phar install --no-ansi --no-interaction
Loading composer repositories with package information
Installing dependencies
[RuntimeException]
Could not load package aws/aws-sdk-php in http://packagist.org: [UnexpectedValueException] Could not parse version constraint ^5.3: Invalid version string "^5.3"
[UnexpectedValueException]
Could not parse version constraint ^5.3: Invalid version string "^5.3"
install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-custom-installers] [--no-scripts] [--no-progress] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader]
2015-05-28 09:57:18,414 [ERROR] (15056 MainThread) [directoryHooksExecutor.py-33] [root directoryHooksExecutor error] Script /opt/elasticbeanstalk/hooks/appdeploy/pre/10_composer_install.sh failed with returncode 1
my composer.json is:
{
"require": {
"aws/aws-sdk-php": "2.7.*",
"monolog/monolog": "1.0.*",
"facebook/php-sdk-v4" : "4.0.*",
"ext-curl": "*",
"paypal/sdk-core-php": "v1.4.2",
"paypal/permissions-sdk-php":"v2.5.106",
"paypal/adaptivepayments-sdk-php":"2.*"
}
}
I notice it does want the aws-sdk-php but the version is not 5.3 (which is mentioned in the logs).
5.3 makes me think php version, checking php -v i get
php -v
PHP 5.5.12 (cli) (built: May 20 2014 22:27:36)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
I've tried re-installing older versions that have previously installed fine and they also fail with the same error. This has to be due to the environment. Does anyone know if there have been changes recently.
Create a folder in your root of the project called .ebextensions. Then create a new file in there called 01-composer-install.config with the following content.
commands:
01_update_composer:
command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update
option_settings:
- namespace: aws:elasticbeanstalk:application:environment
option_name: COMPOSER_HOME
value: /root
I just had to update composer using the instructions here:
https://getcomposer.org/download/

AWS PHP SDK with Composer - missing sdk.class.php

Environment: MAC - Mountain Lion
I am trying to use the AWS PHP SDK for a project. I followed the Amazon web site's SDK installation directions (through composer) -- using the following Link to AWS
I created the file compser.json. Contens:
{
"require": {
"aws/aws-sdk-php": "2.*"
}
}
From the command line, I typed:
curl -s "http://getcomposer.org/installer" | php
Then
php composer.phar install
A new directory appeared "vendor" and inside it, the AWS SDK 2 was automatically installed.
The problem is that I am expecting (per the code example I'm trying to follow), I am expecting to see the following file:
vendor/aws/aws-sdk-for-php/sdk.class.php
But it's not there. Could this be referencing an older version of the SDK?
The automatically generated by the "php composer.phar install" command: vendor/autoload.php looks like this:
<?php
// autoload.php generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit25a7292f83dd9a43a459f6c2e51befba::getLoader();
Is it possible that the file: sdk.class.php is valid for version 1 of the SDK, but not version 2?
Entirely correct. sdk.class.php is a file that exists in SDK 1.x, but not 2.x.
The correct instructions are in the SDK2 README.