2016-11-11 97 views
2

我有一個在AWS Elastic Beanstalk上運行的django應用程序。我使用了我通過使用NLTK下載器獲得的nltk語料庫包(stopwords)。在AWS Elastic Beanstalk上使用下載的NLTK數據

對於一個快速入侵,我剛剛在我當前(單個)彈性beanstalk EC2實例上運行nltk下載器,並將需要的語料庫保存到/usr/local/share/nltk_data。這對單個實例起作用,但很明顯,當我的負載平衡器決定創建新實例時,它將被擦除(它在部署中保留下來)。

我的問題是這裏最好的方法是什麼?

我應該將它存儲在S3上,並將其綁定到我的彈性beanstalk上嗎?

或者,編寫一個將由EB配置調用的(python?)腳本會更容易(也更好)爲每個新實例下載並將數據放入應用程序可訪問的文件夾中生活的實例)?這樣,如果我需要添加其他語料庫下載或做python特定或特定於nltk的事情發生在Python中,而不需要手動S3工作?

如果有人支持爲EB配置編寫腳本,一個例子會很棒,我不確定如何完全做到這一點。

謝謝!

回答

1

在這個特定的用例(結合IAM和EC2實例角色)中使用S3確實非常容易。我們可以手動將差異手動同步到現有的s3位置,這樣您的實例在需要時就可以獲得新的數據。但是,即使數據快速變化(nltk語料庫緩慢變化,我認爲它也是如此)。

關鍵是要給你的實例IAM角色,使用Instance Profiles。通過適當的策略,他們可以安全地訪問s3,而無需手動定義aws憑證,或者在需要在實例啓動時訪問AWS CLI的腳本等。

使用實例配置文件對於IAM權限AWS資源,因爲它消除了硬編碼憑證到腳本中,你的git代碼等

然後假定通過apt,畫中畫等在Linux上安裝AWS CLI:

# create the bucket (once). 
# put in a region/az where your ec2 instances are 
# to minimize data xfer 

# can run these from wherever to get your bucket/data up 
aws s3 mb s3://mybucket --region us-west-1 

# sync from wherever the first time & whenever needed 
aws s3 sync /usr/local/share/nltk_data s3://mybucket 


# can run the below on your instances 
# 
# put instance startup script after install of awscli etc. 
# or in myscript.sh file on your instance (even a gist) 
# wherever you want an instance to have your data or sync up 

aws s3 sync s3://mybucket/nltk_data /path/where/i/need 

有關的好處sync命令是它不會複製未修改的文件當把s3拉下來。這使得它像常見的數據集,備份等東西超級方便。

+0

你好,非常感謝你,這很有幫助。幸運的是,彈性beanstalk會自動創建一個S3存儲桶以及允許每個實例寫入它的日誌,應用程序版本等的IAM角色和實例配置文件。所以,我遵循你的第一條指令,並在我的S3存儲桶中有我的'nltk_data'。但是,我無法弄清楚在.ebextensions中添加到我的配置中的哪些配置參數將安裝awscli,然後從s3調用同步到實例。你能爲我指出正確的方向嗎?我猜它進入'container_commands'鍵?再次感謝! – ministry

+1

我在這裏發佈了這個問題:http://stackoverflow.com/questions/40560053/how-to-copy-folder-from-s3-to-elastic-beanstalk-instance-on-instance-creation – ministry

0

雖然我最終會測試其他答案是否更普遍適用於更復雜的nltk包,停用詞真的只是一個列表(或一組列表我猜如果你需要多國語言),您可以剪切並粘貼到你的腳本:

>>> from nltk.corpus import stopwords 
>>> stopwordlist = stopwords.words('english') 
>>> print(stopwordlist) 
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now'] 

所以我就乾脆直接將它定義在我的劇本,但不導入任何東西:

stopwordlist = ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now']