|
| 1 | +# Integrating dumb-pypi with AWS Lambda |
| 2 | + |
| 3 | +[AWS Lambda][lambda] is a way to run code ("functions") in response to triggers |
| 4 | +(like a change in an S3 bucket) without running any servers yourself. |
| 5 | + |
| 6 | +dumb-pypi works very well with Lambda; you only need to regenerate the index |
| 7 | +when your list of packages changes (relatively rare), and you can serve the |
| 8 | +generated index without involving dumb-pypi at all. |
| 9 | + |
| 10 | +The steps below walk you through an example AWS Lambda setup where a change in |
| 11 | +a "source" bucket (containing all your packages) automatically triggers |
| 12 | +dumb-pypi to regenerate the index and store it in the "output" bucket. |
| 13 | + |
| 14 | +Depending on if you need to support old pip versions, you may even be able to |
| 15 | +serve your index directly from S3, avoiding running any servers entirely. |
| 16 | + |
| 17 | + |
| 18 | +## Initial deployment |
| 19 | + |
| 20 | +These instructions use the sample code in this directory as the base for the |
| 21 | +Lambda handler. The specifics of your bucket will likely vary; it's expected |
| 22 | +that you may need to adjust configuration options or the code itself to match |
| 23 | +your deployment. |
| 24 | + |
| 25 | +1. Create two S3 buckets, e.g. `dumb-pypi-source` and `dumb-pypi-output`. |
| 26 | + |
| 27 | + The source bucket is where you'll drop Python packages (tarballs, wheels, |
| 28 | + etc.) in a flat listing (all objects at the root of the bucket). |
| 29 | + |
| 30 | + The output bucket will contain the generated index (HTML files) which pip |
| 31 | + uses. |
| 32 | + |
| 33 | +2. Create an IAM role which allows reading from the source bucket and |
| 34 | + reading/writing to the output bucket. Select "Lambda" as the AWS resource |
| 35 | + the role applies to during creation. |
| 36 | + |
| 37 | + Here's an example policy (adjust as needed): |
| 38 | + |
| 39 | + ```json |
| 40 | + { |
| 41 | + "Version": "2012-10-17", |
| 42 | + "Statement": [ |
| 43 | + { |
| 44 | + "Sid": "AllowReadToSourceBucket", |
| 45 | + "Effect": "Allow", |
| 46 | + "Action": [ |
| 47 | + "s3:List*", |
| 48 | + "s3:Get*" |
| 49 | + ], |
| 50 | + "Resource": [ |
| 51 | + "arn:aws:s3:::dumb-pypi-source/*", |
| 52 | + "arn:aws:s3:::dumb-pypi-source" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "Sid": "AllowReadWriteToOutputBucket", |
| 57 | + "Effect": "Allow", |
| 58 | + "Action": [ |
| 59 | + "s3:List*", |
| 60 | + "s3:Get*", |
| 61 | + "s3:PutObject", |
| 62 | + "s3:DeleteObject" |
| 63 | + ], |
| 64 | + "Resource": [ |
| 65 | + "arn:aws:s3:::dumb-pypi-output/*", |
| 66 | + "arn:aws:s3:::dumb-pypi-output" |
| 67 | + ] |
| 68 | + } |
| 69 | + ] |
| 70 | + } |
| 71 | + ``` |
| 72 | + |
| 73 | +3. Adjust `config.json` in this directory as necessary (e.g. update |
| 74 | + source/output bucket and the arguments). You can easily change this stuff |
| 75 | + later. |
| 76 | + |
| 77 | +4. Build the first deployment bundle to upload to Lambda. From this directory, |
| 78 | + just run `make bundle.zip`. |
| 79 | + |
| 80 | +5. Create the function. For example, here's how you might do it with the AWS cli: |
| 81 | + |
| 82 | + ```bash |
| 83 | + aws lambda create-function \ |
| 84 | + --region us-west-1 \ |
| 85 | + --function-name dumb-pypi \ |
| 86 | + --runtime python3.6 \ |
| 87 | + --role arn:aws:iam::XXXXXXXXXXXX:role/dumb-pypi \ |
| 88 | + --handler handler.main \ |
| 89 | + --zip-file fileb://bundle.zip |
| 90 | + ``` |
| 91 | + |
| 92 | + (Replace the role, region, etc. to match your setup.) |
| 93 | + |
| 94 | +6. [Give your S3 source bucket permission][s3-allow-trigger] to trigger your new |
| 95 | + Lambda function. For example: |
| 96 | + |
| 97 | + ```bash |
| 98 | + aws lambda add-permission \ |
| 99 | + --region us-west-1 \ |
| 100 | + --function-name dumb-pypi \ |
| 101 | + --statement-id AllowSourceBucketToTriggerDumbPyPI \ |
| 102 | + --action lambda:InvokeFunction \ |
| 103 | + --principal s3.amazonaws.com \ |
| 104 | + --source-arn arn:aws:s3:::dumb-pypi-source \ |
| 105 | + --source-account XXXXXXXXXXXX |
| 106 | + ``` |
| 107 | + |
| 108 | +7. Set up a trigger so that changes to the source bucket cause the `dumb-pypi` |
| 109 | + function to run and regenerate the index. |
| 110 | + |
| 111 | + The AWS cli is very awkward, the easiest way to do this is to make a file |
| 112 | + like `policy.json` with contents like: |
| 113 | + |
| 114 | + ```json |
| 115 | + { |
| 116 | + "LambdaFunctionConfigurations": [ |
| 117 | + { |
| 118 | + "Id": "NotifyDumbPyPI", |
| 119 | + "LambdaFunctionArn": "arn:aws:lambda:us-west-1:XXXXXXXXXXXX:function:dumb-pypi", |
| 120 | + "Events": ["s3:ObjectCreated:*", "s3:ObjectRemoved:*"] |
| 121 | + } |
| 122 | + ] |
| 123 | + } |
| 124 | + ``` |
| 125 | + |
| 126 | + (Again, replacing the function's ARN as appropriate for your account.) |
| 127 | + |
| 128 | + Then, using the AWS cli, add a "notification configuration" to the source |
| 129 | + bucket: |
| 130 | + |
| 131 | + ```bash |
| 132 | + aws s3api put-bucket-notification-configuration \ |
| 133 | + --bucket dumb-pypi-source \ |
| 134 | + --notification-configuration "$(< policy.json)" |
| 135 | + ``` |
| 136 | + |
| 137 | + |
| 138 | +## Serving from the S3 buckets directly |
| 139 | + |
| 140 | +The whole point of Lambda is to avoid running your own servers, so you might as |
| 141 | +well serve directly from S3 :) |
| 142 | + |
| 143 | +Keep in mind that if you need to support old pip versions, you [can't yet serve |
| 144 | +directly from S3][rationale] because these old versions rely on the PyPI server |
| 145 | +to do package name normalization; see [the README][README] for suggestions on |
| 146 | +how to use nginx to do this normalization. |
| 147 | + |
| 148 | +If you **do** want to serve from S3 directly, it's pretty easy: |
| 149 | + |
| 150 | +1. Enable read access to your source bucket. You can enable this to the public, |
| 151 | + whitelisted only to your company's IPs, etc. |
| 152 | + |
| 153 | + Here's an example policy which whitelists your bucket to everyone: |
| 154 | + |
| 155 | + ```json |
| 156 | + { |
| 157 | + "Version": "2008-10-17", |
| 158 | + "Id": "AllowReadOnlyAccess", |
| 159 | + "Statement": [ |
| 160 | + { |
| 161 | + "Sid": "AllowReadOnlyAccess", |
| 162 | + "Effect": "Allow", |
| 163 | + "Principal": { |
| 164 | + "AWS": "*" |
| 165 | + }, |
| 166 | + "Action": "s3:GetObject", |
| 167 | + "Resource": "arn:aws:s3:::dumb-pypi-source/*" |
| 168 | + } |
| 169 | + ] |
| 170 | + } |
| 171 | + ``` |
| 172 | + |
| 173 | + This will make your source bucket available at a URL like |
| 174 | + `https://dumb-pypi-source.s3.amazonaws.com`. |
| 175 | + |
| 176 | +2. Enable read access to your output bucket. Again, it's up to you who you |
| 177 | + allow; you can use the same example policy from above (just adjust the |
| 178 | + bucket name). |
| 179 | + |
| 180 | +3. Enable static website hosting for your output bucket, and set `index.html` |
| 181 | + as your "Index document". This appears to be the only way to get |
| 182 | + `index.html` to show up when accessing the root of a "directory" in S3. |
| 183 | + |
| 184 | + This will make your output bucket available at a URL like |
| 185 | + `http://dumb-pypi-output.s3-website-us-west-1.amazonaws.com/`. |
| 186 | + |
| 187 | + |
| 188 | +## Updating the code or confi |
| 189 | + |
| 190 | +Any time you update the code or config, you need to re-deploy the bundle to |
| 191 | +Lambda. |
| 192 | + |
| 193 | +1. Run `make deploy.zip` to build a new deployment bundle. |
| 194 | + |
| 195 | +2. Use the AWS cli to update the code for the function: |
| 196 | + |
| 197 | + ```bash. |
| 198 | + aws lambda update-function-code \ |
| 199 | + --function-name dumb-pypi \ |
| 200 | + --zip-file fileb://bundle.zip |
| 201 | + ``` |
| 202 | + |
| 203 | +[lambda]: https://aws.amazon.com/lambda/ |
| 204 | +[rationale]: https://github.com/chriskuehl/dumb-pypi/blob/master/RATIONALE.md |
| 205 | +[s3-allow-trigger]: https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html#grant-destinations-permissions-to-s3 |
| 206 | +[README]: https://github.com/chriskuehl/dumb-pypi/blob/master/README.md |
0 commit comments