如何通过 AWS SAM

时间:2021-04-16 13:53:35

标签: amazon-web-services amazon-s3 lambda amazon-cloudformation aws-sam

我正在尝试为我的 Lambda 函数提供 S3FullAccessPolicy 策略。请注意,目标存储桶未在 template.yaml 中配置 - 它已存在。考虑到 this documentation 中的语法示例,我有三个选项:

1.AWS 托管策略命名:

  Policies:
  - S3FullAccessPolicy

2.AWS SAM 策略模板 (SQSPollerPolicy) 定义:

Policies:
  - S3FullAccessPolicy:
      BucketName: abc-bucket-name    

3.或内联策略文档:

  Policies:
  - Statement:
    ...

在尝试 #1 时,我收到一个错误,提示我似乎需要提供一个 arn。如果是这种情况,我将在哪里提供?错误:

1 validation error detected: Value 'S3FullAccessPolicy' at 'policyArn' failed to satisfy constraint:
 Member must have length greater than or equal to 20

对于#2,我提供了存储桶名称,但它表示该策略“无效”。我试过添加引号并用 arn 替换名称 - 但没有运气。

和 #3 - 我可以找到策略 here 的代码,但这是在 yaml 中,所以我想知道这是否是我应该使用的代码。

我在这里错过了什么?我愿意使用这些选项中的任何一个,但现在我是 0/3。

完整的 Lambda 函数:

  testFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: lambda/testFunction/
      Handler: app.lambda_handler
      Runtime: python3.8
      Timeout: 900
      Policies:
        - S3FullAccessPolicy

1 个答案:

答案 0 :(得分:2)

我使用以下模板没有任何问题。

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Login</title>
</head>
<body>

    <h3>User Login</h3>
    @Model.Msg
    <form method="post" asp-page="Login">
        <table>
            <tr>
                <td>Email Address</td>
                <td><input type="text" asp-for="@Model.Username" /></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" asp-for="@Model.Password" /></td>
            </tr>
            <tr>
                <td>
                    Select whether you are a doctor or patient:
                    <p></p>
                    <input type="radio" id="doc" name="gender" value="Doctor">
                    <label for="doc">Doctor</label><br>
                    <input type="radio" id="pat" name="gender" value="Patient">
                    <label for="pat">Patient</label><br />
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="Login" name="sub"/></td>
            </tr>
        </table>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TeleHealthB.Models;


namespace TeleHealthB.Pages
{
    public class LogInModel : PageModel
    {
       
        [BindProperty]
        public string Username { get; set; }

        [BindProperty]
        public string Password { get; set; }

        public string Msg { get; set; }

        public void OnGet()
        {
        }

        
        public IActionResult OnPost(string sub )
        {
            using (var context = new HealthProjectContext())
            {
                try

                {
                    var query = from st in context.Patients
                                where st.Email == Username
                                select st.Password;

                    string check2 = query.FirstOrDefault();
                     

                   
                    if (Password.Equals(check2.Trim()))
                    {
                        HttpContext.Session.SetString("Username", Username);
                        return RedirectToPage("Welcome");
                       
                    }
                    else
                    {
                        Msg = "Invalid";
                        return Page();
                    }
                }
                catch
                {
                    Msg = "Invalid";
                    return Page();
                }
            }
        }
    }
}

使用以下命令运行它并成功部署。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31


Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8
      Tracing: Active
      Policies:
        - S3FullAccessPolicy:
            BucketName: existingbucketname # bucket name without arn