我想通过c#控制台应用程序部署ASP.Net Core Web应用程序项目。这意味着我试图在没有AWS Toolkit或CLI命令的情况下在AWS上创建无服务器lambda应用程序。我想使用AWS开发工具包做到这一点。
为此,我在Nugets下面添加了:
第一步,我使用msbuild命令制作一个软件包。然后压缩并上传到S3存储桶,最后一步,我在下面的代码下运行。它不会显示任何错误,但不在我的功能列表中。
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应用程序。
答案 0 :(得分:3)
我必须使用cloudFormation请求和响应来部署这种项目。 AWS开发工具包具有许多不错的类和函数。
所以我开发了一些对我有用的方法:
CheckAndFillBucketName();
CheckAndFillStackName();
CheckAndFillRegion();
MakePackage();
ZipPackage(zipPublishFolder, packageFileName);
UploadPackageToS3(zipPublishFolder, packageFileName);
UploadTemplateToS3(packageFileName);
var cloudFormation = new AWSCloudFormation(tempstack, templateUrl);
cloudFormation.CreateCloudFormationOnAWS();
为了创建CloudFormation对象,我创建了一个像这样的类:
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;
}
}
}