Link

AWS 全托管服务

目录

  1. AWS 全托管服务
    1. 参考文档
    2. AWS Directory Service
    3. AWS Fargate
    4. Lambda
    5. API gateway
    6. 无服务实验 Lambda

参考文档

http://d0.awsstatic.com/whitepapers/aws-overview.pdf

AWS Directory Service

AWS 中托管的 Microsoft Active Directory

可以支持目录感知型工作负载和各种 AWS 资源,以在 AWS 中使用托管的 Active Directory (AD)。AWS Managed Microsoft AD 基于实际的 Microsoft AD 构建,不需要您将数据从现有 Active Directory 同步或复制到云中。您可以使用标准的 AD 管理工具,还可利用各种内置的 AD 功能,例如组策略和单点登录。借助 AWS Managed Microsoft AD,您可以将 Amazon EC2 和 Amazon RDS for SQL Server 实例轻松加入到域中,并使用 Amazon WorkSpaces 等 AWS 最终用户计算 (EUC) 服务以及 AD 用户和组。

AWS Fargate

完全托管的容器服务。支持 ECS 和 EKS。

使用 Fargate 后,无需管理运行容器的节点和集群。

Lambda

完全托管的计算服务,运行无状态代码。

支持 Node.JS、Java、Python、C#、Go、Ruby 等语言。

可以按计划或者针对事件运行代码。

API gateway

每秒支持 10w 的 API 并发请求。

可以处理下列工作负载:

  • EC2
  • Lambda
  • 任何 Web 应用

API 支持通过多种方式进行访问控制:

https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-to-api.html

  • resource policy:设置来自某些 IP/endpoint 的资源可以访问 API
  • IAM role 和 policy:哪些 IAM role 可以创建、管理和使用 API
  • IAM tag:与 IAM policy 协同使用
  • Lambda authorizers:通过 lambda 函数控制访问
  • Amazon Cognito user pools
  • WAF

为什么要使用 Amazon API Gateway?

Amazon API Gateway 为开发人员提供了一种简单灵活的完全托管服务,此服务按使用量计费,可以处理为应用程序后端创建并操作功能强大的 API 的各个方面事务。通过 API Gateway,您可以更快速地启动新服务并降低投资,从而专注于构建核心业务服务。API Gateway 专为帮助您处理创建和管理 API 的多个方面而构建:

1) 计量。API Gateway 可帮助您制定用于计量和限制第三方开发人员访问 API 的计划。您可以依据每个 API 密钥定义一组计划、配置限制和设定配额限制。API Gateway 自动计量流向 API 的流量并允许您提取每个 API 密匙的使用数据。

2) 安全性。API Gateway 可为您提供多种授权访问 API 和控制服务操作访问的工具。API Gateway 使您可以利用多种 AWS 管理和安全工具 (比如 AWS Identity and Access Management (IAM) 和 Amazon Cognito) 来授权访问您的 API。API Gateway 可以通过 AWS 用于其自身 API 的相同方法来代表您验证签名的 API 调用。借助以 AWS Lambda 函数形式编写的自定义授权命令,API Gateway 还可以帮助您验证传入不记名令牌,从而消除后端代码的授权问题。

3) 弹性。API Gateway 可帮助您通过限制来管理流量,从而让后端操作可以抵挡流量高峰。通过缓存 API 调用的输出来避免每次都调用后端,API Gateway 还可帮助您提升 API 性能,并缩短最终用户遇到的延迟。

4) 操作监控。在 API 发布并处于使用状态后,API Gateway 便会为您提供指标控制面板,监控对您服务的调用情况。API Gateway 控制面板通过与 Amazon CloudWatch 集成为您提供各种后端性能指标,涵盖 API 调用次数、延迟数据和错误率。您可以为 API 中每种方法启用详细指标,也可以在 CloudWatch 日志中接收错误日志、访问日志或调试日志。

5) 生命周期管理。在发布了 API 之后,您通常需要构建和测试增强现有功能或添加新功能的新版本。API Gateway 使您可以同时操作每个版本的多个 API 版本和多个阶段,以便在发布新 API 版本之后,现有应用可以继续调用先前的版本。

6) 专为开发人员设计。API Gateway 使您可以快速创建 API 并为其响应分配静态内容,以降低您的应用程序的跨团队开发工作和面市时间。依赖于您的 API 的团队可以在您构建后端进程的同时开始进行开发。

7) 实时双向通信。 构建实时双向通信应用程序,如聊天应用程序、流式处理控制面板和通知,无需运行或管理任何服务器。API Gateway 可以在互连用户之间维持永久连接,并支持互连用户之间的消息传输。

无服务实验 Lambda

工作流程

当 S3 上有文件更新时,从 S3 加载库存到 DynamoDB,通过额外的页面进行展示。

# Load-Inventory Lambda function
#
# This function is triggered by an object being created in an Amazon S3 bucket.
# The file is downloaded and each line is inserted into a DynamoDB table.

import json, urllib, boto3, csv

# Connect to S3 and DynamoDB
s3 = boto3.resource('s3')
dynamodb = boto3.resource('dynamodb')

# Connect to the DynamoDB tables
inventoryTable = dynamodb.Table('Inventory');

# This handler is run every time the Lambda function is triggered
def lambda_handler(event, context):

  # Show the incoming event in the debug log
  print("Event received by Lambda function: " + json.dumps(event, indent=2))

  # Get the bucket and object key from the event
  bucket = event['Records'][0]['s3']['bucket']['name']
  key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'])
  localFilename = '/tmp/inventory.txt'

  # Download the file from S3 to the local filesystem
  try:
    s3.meta.client.download_file(bucket, key, localFilename)
  except Exception as e:
    print(e)
    print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
    raise e

  # Read the Inventory CSV file
  with open(localFilename) as csvfile:
    reader = csv.DictReader(csvfile, delimiter=',')

    # Read each row in the file
    rowCount = 0
    for row in reader:
      rowCount += 1

      # Show the row in the debug log
      print(row['store'], row['item'], row['count'])

      try:
        # Insert Store, Item, and Count into the Inventory table
        inventoryTable.put_item(
          Item={
            'Store':  row['store'],
            'Item':   row['item'],
            'Count':  int(row['count'])})

      except Exception as e:
         print(e)
         print("Unable to insert data into DynamoDB table".format(e))

    # Finished!
    return "%d counts inserted" % rowCount

库存不足时通知函数,当 DynamoDB 有表项变动时触发此函数,检查库存是否为 0 ,然后发送邮件通知(topic:NoStock)。

# Stock Check Lambda function
#
# This function is triggered when values are inserted into the Inventory DynamoDB table.
# Inventory counts are checked, and if an item is out of stock, a notification is sent to an SNS topic.

import json, boto3

# This handler is run every time the Lambda function is triggered
def lambda_handler(event, context):

  # Show the incoming event in the debug log
  print("Event received by Lambda function: " + json.dumps(event, indent=2))

  # For each inventory item added, check if the count is zero
  for record in event['Records']:
    newImage = record['dynamodb'].get('NewImage', None)
    if newImage:

      count = int(record['dynamodb']['NewImage']['Count']['N'])

      if count == 0:
        store = record['dynamodb']['NewImage']['Store']['S']
        item  = record['dynamodb']['NewImage']['Item']['S']

        # Construct message to be sent
        message = store + ' is out of stock of ' + item
        print(message)

        # Connect to SNS
        sns = boto3.client('sns')
        alertTopic = 'NoStock'
        snsTopicArn = [t['TopicArn'] for t in sns.list_topics()['Topics']
                        if t['TopicArn'].lower().endswith(':' + alertTopic.lower())][0]

        # Send message to SNS
        sns.publish(
          TopicArn=snsTopicArn,
          Message=message,
          Subject='Inventory Alert!',
          MessageStructure='raw'
        )

  # Finished!
  return 'Successfully processed {} records.'.format(len(event['Records']))