问题描述
我想通过c#控制台应用程序部署我的ASP.Net Core Web应用程序项目.这意味着我试图在没有AWS Toolkit或CLI命令的情况下在AWS上创建无服务器lambda应用程序.我想使用AWS开发工具包做到这一点.
为此,我在Nugets下方添加:
I want to deploy my ASP.Net Core Web Application project through a c# console application. It means I am trying to create a serverless lambda application on AWS without AWS Toolkit or CLI command. I want to do this with AWS SDK.
For this purpose I added below Nugets :
- AWSSDK.ServerlessApplicationRepository
- AWSSDK.Core
- AWSSDK.Lambda
第一步,我使用msbuild命令制作一个软件包.然后压缩并上传到S3存储桶,最后一步,我在下面的代码下运行.它不会显示任何错误,但不在我的功能列表中.
at first step I am making a package with msbuild command. Then Zipped and upload on S3 bucket and in final step I am running below code. It does not show me any error but it is not in my function list.
AmazonServerlessApplicationRepositoryClient clie = new AmazonServerlessApplicationRepositoryClient("AKIAJQVBDUUDGLXOEKYA", "HdoCIeKqtnKYVXB6y/HHnK6mTD2G556jqAp+bk3e", RegionEndpoint.EUWest1);
CreateApplicationRequest createApplicationRequestObject = new CreateApplicationRequest()
{
Name = "ApplicationTest",
Author = "Mike",
Description = "Mike Desc",
SourceCodeUrl = "https://region/bucketname/publishfolder/" + packageFileName,
};
CreateApplicationResponse createApplicationResponseObject = clie.CreateApplication(createApplicationRequestObject);
我在CreateApplicationResponse对象中找不到任何错误.同样创建了CreateApplicationResponse对象的状态属性,但是在Lambda->控制台的应用程序中找不到任何新的Lambda应用程序.
I can't find any any error in the CreateApplicationResponse object. Also status proeprty of the CreateApplicationResponse object is Created but I can not find any new Lambda application in Lambda->Application of console.
推荐答案
我必须使用cloudFormation请求和响应来部署此类项目. AWS开发工具包具有许多不错的类和功能.
因此,我开发了一些对我有用的方法:
I had to use cloudFormation request and responses for deploying this kind of project. AWS SDK has many nice classes and functions that do it.
So I developed some methods that do it for me :
CheckAndFillBucketName();
CheckAndFillStackName();
CheckAndFillRegion();
MakePackage();
ZipPackage(zipPublishFolder, packageFileName);
UploadPackageToS3(zipPublishFolder, packageFileName);
UploadTemplateToS3(packageFileName);
var cloudFormation = new AWSCloudFormation(tempstack, templateUrl);
cloudFormation.CreateCloudFormationOnAWS();
为了创建CloudFormation对象,我创建了一个像这样的类:
for creating CloudFormation object, I created a class like this that do it for me :
public class AWSCloudFormation
{
public string StackName { get; set; }
public string TemplateUrl { get; set; }
AmazonCloudFormationClient CloudFormationClient;
public AWSCloudFormation(string stackName, string templateUrl)
{
CloudFormationClient = CreateCloudFormationClient();
StackName = stackName;
TemplateUrl = templateUrl;
}
public AmazonCloudFormationClient CreateCloudFormationClient()
{
var amazonCloudFormationConfig = new AmazonCloudFormationConfig
{
RegionEndpoint = RegionEndpoint.GetBySystemName(Program.AWSLambdaToolsJsonConfig.Region),
};
return new AmazonCloudFormationClient(Program.AccessKey, Program.SecretKey, amazonCloudFormationConfig);
}
static Stack GetStack(AmazonCloudFormationClient cloudFormationClient, string name)
{
return cloudFormationClient.DescribeStacks(new DescribeStacksRequest { StackName = name }).Stacks.First();
}
public void CreateCloudFormationOnAWS()
{
try
{
Log.Info(Program.LogPath, "Creating Cloud Information");
var describeStacksRequest = new DescribeStacksRequest();
var changeSetName = "changeset" + Program.PostfixExpression;
var changeSetType = ChangeSetType.CREATE;
if (CheckStackIsExist(CloudFormationClient, StackName))
{
changeSetType = ChangeSetType.UPDATE;
}
var createChangeSetRequest = new CreateChangeSetRequest
{
ChangeSetName = changeSetName,
StackName = StackName,
//TemplateBody = ServerlessTemplateBody,
TemplateURL = TemplateUrl,
ChangeSetType = changeSetType,
Capabilities = new List<string> { "CAPABILITY_IAM" },
};
var createChangeSetResponse = CloudFormationClient.CreateChangeSet(createChangeSetRequest);
WaitForChangeSet(CloudFormationClient, StackName, changeSetName);
var executeChangeSetResponse = CloudFormationClient.ExecuteChangeSet(new ExecuteChangeSetRequest
{
ChangeSetName = changeSetName,
StackName = StackName,
});
WaitForStack(CloudFormationClient, StackName);
var generatedStack = GetStack(CloudFormationClient, StackName);
Log.Info(Program.LogPath, "Output URL is : " + generatedStack.Outputs.Find(x => x.OutputKey == "ApiURL").OutputValue);
Log.Info(Program.LogPath, "Creating Cloud Information Finished");
}
catch (Exception ex)
{
Log.Error(Program.LogPath.FullName, "Creating Cloud Information Error : " + ex.Message);
}
}
static void WaitForChangeSet(AmazonCloudFormationClient amazonCloudFormationClient, string stackName, string changeSetName)
{
var status = ChangeSetStatus.CREATE_PENDING;
while (status != ChangeSetStatus.CREATE_COMPLETE)
{
var changeSet = amazonCloudFormationClient.DescribeChangeSet(new DescribeChangeSetRequest { StackName = stackName, ChangeSetName = changeSetName });
status = changeSet.Status;
Log.Info(Program.LogPath, $"Changeset '{changeSetName}' (In Stack : {stackName}) status is {changeSet.Status} at {DateTime.Now.TimeOfDay}");
if (status != ChangeSetStatus.CREATE_COMPLETE) Thread.Sleep(TimeSpan.FromSeconds(10));
}
}
static void WaitForStack(AmazonCloudFormationClient amazonCloudFormationClient, string stackName)
{
var stack = GetStack(amazonCloudFormationClient, stackName);
var status = stack.StackStatus;
string statusReason = null;
while (status == StackStatus.CREATE_IN_PROGRESS ||
status == StackStatus.UPDATE_IN_PROGRESS ||
status == StackStatus.UPDATE_ROLLBACK_IN_PROGRESS ||
status == StackStatus.ROLLBACK_IN_PROGRESS ||
status == StackStatus.UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS ||
status == StackStatus.UPDATE_COMPLETE_CLEANUP_IN_PROGRESS ||
status == StackStatus.REVIEW_IN_PROGRESS)
{
stack = GetStack(amazonCloudFormationClient, stackName);
status = stack.StackStatus;
statusReason = stack.StackStatusReason;
Log.Info(Program.LogPath, $"Stack '{stackName}' status is {status} because {statusReason} at {DateTime.Now.TimeOfDay}");
if (status == StackStatus.CREATE_IN_PROGRESS || status == StackStatus.UPDATE_IN_PROGRESS) Thread.Sleep(TimeSpan.FromSeconds(10));
}
if (status != StackStatus.CREATE_COMPLETE &&
status != StackStatus.UPDATE_COMPLETE &&
status != StackStatus.ROLLBACK_COMPLETE &&
status != StackStatus.UPDATE_ROLLBACK_COMPLETE)
{
var eventsResponse = amazonCloudFormationClient.DescribeStackEvents(new DescribeStackEventsRequest { StackName = stackName });
throw new FailedToCreateStackException(stackName, RegionEndpoint.GetBySystemName(Program.AWSLambdaToolsJsonConfig.Region), status.Value, statusReason, eventsResponse.StackEvents);
}
}
static bool CheckStackIsExist(AmazonCloudFormationClient amazonCloudFormationClient, string stackName)
{
try
{
var stack =
amazonCloudFormationClient.DescribeStacks(new DescribeStacksRequest { StackName = stackName }).Stacks.First();
if (stack != null)
return true;
return false;
}
catch
{
return false;
}
}
}
这篇关于使用AmazonServerlessApplicationRepositoryClient部署AWS Serverless lambda应用程序无法正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!