【问题标题】:Deploying AWS Serverless lambda Application with AmazonServerlessApplicationRepositoryClient does not work?使用 AmazonServerlessApplicationRepositoryClient 部署 AWS 无服务器 lambda 应用程序不起作用?
【发布时间】:2019-03-26 18:00:08
【问题描述】:

我想通过 c# 控制台应用程序部署我的 ASP.Net Core Web 应用程序项目。这意味着我正在尝试在没有 AWS Toolkit 或 CLI 命令的情况下在 AWS 上创建一个无服务器 lambda 应用程序。我想用 AWS 开发工具包做到这一点。
为此,我在 Nugets 下面添加了:

  • AWSSDK.ServerlessApplicationRepository
  • AWSSDK.Core
  • AWSSDK.Lambda

第一步,我正在使用 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 对象的状态属性也是 Created 但我在 Lambda->Application of console 中找不到任何新的 Lambda 应用程序。

【问题讨论】:

    标签: c# aws-lambda aws-serverless


    【解决方案1】:

    我必须使用 cloudFormation 请求和响应来部署此类项目。 AWS SDK 有许多很好的类和函数来做这件事。
    所以我开发了一些方法来为我做这件事:

    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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-09
      • 2019-10-12
      • 2019-08-04
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多